HELP ! Converting an ASP page to PHP

Dorn's picture

They have: 42 posts

Joined: Jul 2001

I am trying to get an ASP File to work as PHP.
I cant seem to get it to work....

Here is the code that I have made and attempt at:

<?
//Dim dbConnection, strConnect, dbRecordSet
//Dim strQuery, intField


//----------------------------------------------------------------------------------------------------
//Subprocedures
//----------------------------------------------------------------------------------------------------  
   //Open a database connection
//   Sub OpenDbase()
//      set dbConnection  = server.createobject("adodb.connection")     
//      strConnect = "Driver={Microsoft Access Driver (*.mdb)};DBQ="
//      strConnect = strConnect & Server.MapPath("Career Oppotunities.mdb")
//      dbConnection.open   strConnect
//   End Sub

$cfg_dsn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=Career Oppotunities.mdb; UserCommitSync=Yes;
Threads=3; SafeTransactions=0; PageTimeout=5; MaxScanRows=8; MaxBufferSize=2048; DriverId=281; DefaultDir=C:/ProgramFiles/CommonFiles/ODBC/DataSources";

$cfg_dsn_login = "";
$cfg_dsn_mdp = "";

odbc_connect($cfg_dsn,$cfg_dsn_login,$cfg_dsn_mdp);


$sqlString = "Select [DEPARTMENT], [POSITION TITLE], [POS #], [STS], [SHIFT], [PAY RANGE], [PAY GRADE], [DATE POSTED]  from [Career Opportunities] order by [DEPARTMENT]";
$result = mysql_query($sqlString);
//$dbRecordSet = dbConnection.execute(strQuery)

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/09.22.04 Intranet.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<title>FMC</title>
</head>
<body>
<div id="container">
<div id="header"></div>
<div id="content">
  <div align="center">
  <? //$strDate = Date() ?>
    <p style="font-size: x-small; color: #006699;"><%=strDate%></p>
    </div>
  <div>
    <table align="center" cellpadding="3" cellspacing="0" id="menu_table">
  <thead>
    <tr>
      <td><div align="center"><b>DEPARTMENT</b></div></td>
      <td><div align="center"><b>POSITION TITLE</b></div></td>
      <td><div align="center"><b>POS #</b></div></td>
      <td><div align="center"><b>STS</b></div></td>
      <td><div align="center"><b>SHIFT</b></div></td>
      <td><div align="center"><b>PAY RANGE</b></div></td>
      <td><div align="center"><b>PAY GRADE</b></div></td>
      <td><div align="center"><b>DATE POSTED</b></div></td>
    </tr>
  </thead>
<?

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
//do while not dbRecordset.eof
echo "<tr>";
echo "<td bgcolor='#F4F4F4'><div align=center>".$row['DEPARTMENT']."</td>";
echo "<td><div align=center>".$row['POSITION TITLE']."</td>";
echo "<td bgcolor='#F4F4F4'><div align=center>".$row['POS #']."</td>";
echo "<td><div align=center>".$row['STS']."</td>";
echo "<td bgcolor='#F4F4F4'><div align=center>".$row['SHIFT']."</td>";
echo "<td><div align=center>".$row['PAY RANGE']."</td>";
echo "<td bgcolor='#F4F4F4'><div align=center>".$row['PAY GRADE']."</td>";
echo "<td><div align=center>".$row['DATE POSTED']."</td>";
echo "</tr>";
}
?>
</table>
*   
  </div>
</div>
</body>
<!-- InstanceEnd --></html>
'

I know NO ASP and not a TON of PHP.

Can someone help!?!?!

I attempted to cut out all code that had nothing to do with what I am trying to do for clarity sake.
Thanks

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Is this the ASP code you started with, or the PHP you are finishing with?

I see code for a connection string to a MS access file, yet there are commands for mySQL in there as well.

Which database will you be using with PHP?

-Greg

Dorn's picture

They have: 42 posts

Joined: Jul 2001

I cant change the file type. So it has to stay a MS access file...

Busy's picture

He has: 6,151 posts

Joined: May 2001

In your select statement, take off the square brackets, should be: select DEPARTMENT, POSITION TITLE etc (if you are selecting everything you can use * which is a wildcard meaning everything - select * from ...)

<?php
=strDate
?>
, unless defined somewhere else needs to be <?=$strDate?>, there is an option to use ASP tags in PHP but am unsure ifyou have that set or not.

you also need to make a connection to your database, what type are you using? MSSQL, MYSQL, ...

might also want to watch out for #, as it's a comment in PHP (3 methods)
// is a comment
/* is a
mutli line comment */
# is a comment

Dorn's picture

They have: 42 posts

Joined: Jul 2001

The file that I need to display is a MS access file. It sits in the same directory as the index page. I have the ASP page that works but the site was just converted to PHP. There are several include files that will be required so this page has to be PHP...

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Well if you are using an access file, then definately one big issue is your use of the mysql functions (mysql_fetch_array and mysql_query) There apears to be similar ones for ODBC, see http://us2.php.net/manual/en/ref.uodbc.php for all the ODBC functions.

Also, the server you are running it on, what operating system is it? Check out the second comment down at the bottom of http://us2.php.net/manual/en/function.odbc-connect.php someone posted the instructions it looks like for accessing a access database.

I didn't try it, but it looked like a pretty good sample with instructions how how to set it up on the server. (in that case, using a win server).

-Greg

PS. You mentioned you needed to go to PHP becasue you had include files. Are these files done in PHP or you just need the ability to include another file? If it is just the ability is needed, I'm sure ASP has something to do that as well.

Dorn's picture

They have: 42 posts

Joined: Jul 2001

it is on an IIS server...
Here is what I am trying to do.
There is a Microsoft Access database that is kept in the same directory that the index.php/asp file is. The index page needs to pull the data from that and display it in a table. I have the ASP page that does work but there is no way for me to include the required includes which are PHP.

I am willing to pay someone $20 (or more if needed) if they can help me get it to work. I have spent much to much time on this one file and have not gotten any closer.

I dont know both languages enough to make it work.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Unfortuently, I do not have access to a Windows server to test the following, but give it a try to see if it works.

The connection string, I just left the way you had it, just formatted it in a way that is easier to read. (My personal pref). I don't know if it is right since I don't have a way to test it. For now, we'll assume it's right until you can test it.

This code has error reporting that should give a better idea of where it is failing and possibly why.

Let me know what errors you get with this version.

-Greg

<?php
 
// For a display such as 3/4/2004 use this one:
 
$strDate = date(\"n/j/Y\");

  // For a display such as 03/04/2004 use this one:
  //
$strDate = date(\"m/d/Y\");


<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<!-- InstanceBegin template=\"/Templates/09.22.04 Intranet.dwt\" codeOutsideHTMLIsLocked=\"false\" -->

<head>
  <title>FMC</title>
</head>

<body>
  <div id=\"container\">
    <div id=\"header\"></div>       
    <div id=\"content\">
      <div align=\"center\">
        <p style=\"font-size: x-small; color: #006699;\">=
$strDate </p>
      </div>
      <div>
        <table align=\"center\" cellpadding=\"3\" cellspacing=\"0\" id=\"menu_table\">
          <thead>
            <tr>
              <td><div align=\"center\"><b>DEPARTMENT</b></div></td>
              <td><div align=\"center\"><b>POSITION TITLE</b></div></td>
              <td><div align=\"center\"><b>POS #</b></div></td>
              <td><div align=\"center\"><b>STS</b></div></td>
              <td><div align=\"center\"><b>SHIFT</b></div></td>
              <td><div align=\"center\"><b>PAY RANGE</b></div></td>
              <td><div align=\"center\"><b>PAY GRADE</b></div></td>
              <td><div align=\"center\"><b>DATE POSTED</b></div></td>
            </tr>
          </thead>
         
         
           
$cfg_dsn = \"DRIVER=Microsoft Access Driver (*.mdb);\" .
                       \"DBQ=Career Oppotunities.mdb;\" .
                       \"UserCommitSync=Yes;\" .
                       \"Threads=3;\" .
                       \"SafeTransactions=0;\" .
                       \"PageTimeout=5;\" .
                       \"MaxScanRows=8;\" .
                       \"MaxBufferSize=2048;\" .
                       \"DriverId=281;\" .
                       \"DefaultDir=C:/ProgramFiles/CommonFiles/ODBC/DataSources\";
                       
           
$cfg_dsn_login = \"\";
           
$cfg_dsn_mdp = \"\";
           
           
$dbConn = odbc_connect($cfg_dsn,$cfg_dsn_login,$cfg_dsn_mdp);
           
            if (
$dbConn)
            {
              // Connection was made, continue
             
             
$sqlString = \"Select [DEPARTMENT], \" .
                           \"       [POSITION TITLE], \" .
                           \"       [POS #], \" .
                           \"       [STS], \" .
                           \"       [SHIFT], \" .
                           \"       [PAY RANGE], \" .
                           \"       [PAY GRADE], \" .
                           \"       [DATE POSTED] \" .
                           \"From [Career Opportunities] \" .
                           \"Order By [DEPARTMENT]\";
                        
             
$dbResult = odbc_exec($dbConn,$sqlString);
             
              if (
$dbResult)
              {
                // Results were found, process them
               
                while (odbc_fetch_row(
$dbResult))
                {
                 
$thisDept     = odbc_result($dbResult, \"DEPARTMENT\");
                 
$thisPos      = odbc_result($dbResult, \"POSITION TITLE\");
                 
$thisPosNo    = odbc_result($dbResult, \"POS #\");
                 
$thisSTS      = odbc_result($dbResult, \"STS\");
                 
$thisShift    = odbc_result($dbResult, \"SHIFT\");
                 
$thisPayRange = odbc_result($dbResult, \"PAY RANGE\");
                 
$thisPayGrade = odbc_result($dbResult, \"PAY GRADE\");
                 
$thisDatePost = odbc_result($dbResult, \"DATE POSTED\");
     
                  echo \"<tr>\n\";
                  echo \"<td bgcolor='#F4F4F4'><div align=center>\" .
$thisDept . \"</td>\n\";
                  echo \"<td><div align=center>\" .
$thisPos . \"</td>\n\";
                  echo \"<td bgcolor='#F4F4F4'><div align=center>\" .
$thisPosNo . \"</td>\n\";
                  echo \"<td><div align=center>\" .
$thisSTS . \"</td>\n\";
                  echo \"<td bgcolor='#F4F4F4'><div align=center>\" .
$thisShift . \"</td>\n\";
                  echo \"<td><div align=center>\" .
$thisPayRange . \"</td>\n\";
                  echo \"<td bgcolor='#F4F4F4'><div align=center>\" .
$thisPayGrade . \"</td>\n\";
                  echo \"<td><div align=center>\" .
$thisDatePost . \"</td>\n\";
                  echo \"</tr>\";
                }
              }
              else
              {
                // No results were found
               
                echo \"<tr>\n\";
                echo \"<td colspan='8'>** NO RECORDS FOUND**</td>\n\";
                echo \"</tr>\n\";
              }
             
              odbc_close(
$dbConn);
            }
            else
            {
              // Connection to database failed

              echo \"<tr>\n\";
              echo \"<td colspan='8'>DB Connection Failed:<br>\" . odbc_errormsg() . \"</td>\n\";
              echo \"</tr>\n\";
            }
         
         
        </table>
      </div>
  </div>
</body>

<!-- InstanceEnd -->

</html>
?>

Dorn's picture

They have: 42 posts

Joined: Jul 2001

Thank you!

I got no result with the code... I used what was provided exactly and the result page was blank. Not sure what is happening.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Hmm. I placed it on a server I use jsut to check to make sure the code itself looked ok (I knew the connection wouldn't work).

I got a page with the date at the top, the table headings, and then an error stating DB Connection Failed: [iODBC][Driver Manager]Data source name not found and no default driver specified. Driver could not be loaded (this I expected)

When the page loads for you, if you do a view source is there anything at all??

-Greg

Dorn's picture

They have: 42 posts

Joined: Jul 2001

there is nothing... I checked to see if it was on my code and when I took all of what was provided out, the template worked again.

Could it be that php on the server is screwy?

Troy1960's picture

They have: 152 posts

Joined: Sep 2004

You can convert asp to php using this tool.

asp2php.naken.cc/

Dorn's picture

They have: 42 posts

Joined: Jul 2001

bad link

Troy1960's picture

They have: 152 posts

Joined: Sep 2004

http://asp2php.naken.cc/

Try that one.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.