Page 1 of 1

trouble converting asp code to php

Posted: Thu Jun 01, 2006 9:11 am
by gcapp
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am a newbie to php. I have used asp for years.  I have some asp code that I am trying to convert to php.  I think I have got most of the code converted correctly, however, I am getting a T_STRING parse error.

I thought someone might glance at the code below and tell me if they see anything wrong.  What has taken me days, I'm sure will take an expert seconds.

If there are any errors anywhere - could someone correct me??

Here is the code I have converted:

The code between the ********************** marks is where I am getting the parse error.

Code: Select all

<?php

$db = 'C:\\Inetpub\\wwwroot\\accord\\database\\Accord.mdb';

$conn = new COM('ADODB.Connection') or exit('Cannot start ADO.');
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db") or exit('Cannot open with Jet.');

$sParent = $temp=Trim(Request("Parent"));
If ($sParent == "") {
 $sParent = Null;
}

$CATEGORY_TABLE = "Page_category";				
$INFO_TABLE = "PageInfo";						


$sql="SELECT PageInfo.ID, PageInfo.Description FROM PageInfo INNER JOIN Page_category ON PageInfo.Category = Page_category.Category_ID WHERE PageInfo.Category LIKE '%" & sParent & "%';

$rs = $conn->Execute($sql);
	
******************************************************	
'Existence Text
	If (!$rs->EOF)  {
		echo "Sorry, there are no pages listed for this category.";
	        }
        Else   {
		echo "Here are the pages for this category.";
                }

******************************************************
	
?>
'Loop through members
<?php while (!$rs->EOF) { ?>

    echo($rs("Description") & "")

	
	<?php $rs->MoveNext() ?>

<?php } ?>

<?

$rs->Close();
$conn->Close();

$rs = null;
$conn = null;

?>

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jun 01, 2006 9:16 am
by ok
You write comments like that:

Code: Select all

//Notes...
You forgot to close the query...

Code: Select all

$sql="SELECT PageInfo.ID, PageInfo.Description FROM PageInfo INNER JOIN Page_category ON PageInfo.Category = Page_category.Category_ID WHERE PageInfo.Category LIKE '%" & sParent & "%'";

Posted: Thu Jun 01, 2006 9:19 am
by shiznatix
remove this:

'Existence Text

and this:

'Loop through members

because thats not php. php comments are started like this:

Code: Select all

//comment

/*
multiline comment
*/

#very seldom used comment
you might have problems because your code is well...a bit messy. example:

Code: Select all

?>
'Loop through members
<?php while (!$rs->EOF) { ?>

echo($rs("Description") & "")


<?php $rs->MoveNext() ?>

<?php } ?>

<?

$rs->Close();
$conn->Close();

$rs = null;
$conn = null;

?>
can be turned into:

Code: Select all

?>
<?php
while (!$rs->EOF)
{
    echo $rs["Description"];
    $rs->MoveNext();
}

$rs->Close();
$conn->Close();

$rs = null;
$conn = null;

?>
i would recommend reading a coding standards site that will tell you how to write code according to standards.

that was just a quick look as i am sure there are more errors.

Posted: Thu Jun 01, 2006 9:19 am
by Weirdan
  • as you can guess from syntax highlighting, you have get the quotes wrong in your sql query
  • string concatenation in php is done using the . (dot) operator:

    Code: Select all

    $var = "string1" . "string2";
  • comments either start with // or #, or enclosed in /* and */

Posted: Thu Jun 01, 2006 12:05 pm
by gcapp
Thanks to all who helped with my code. I cleaned all of it up and now I get a error depending on which $conn I use:

If I use the Jet connection, the error I get is:

Cannot connect to Jet

If I use the Access driver connection, the error I get is:

Cannot open with driver

My php code is here:

Code: Select all

<?php

$db = 'C:\\Inetpub\\wwwroot\\accord\\database\\Accord.mdb';

$conn = new COM('ADODB.Connection') or exit('Cannot start ADO.');
$conn->Open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db') or exit('Cannot open with Jet.');
//$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db") or exit('Cannot open with driver.');


$sParent = $temp=Trim(Request('Parent'));
If ($sParent == "") {
 $sParent = Null;
}

$CATEGORY_TABLE = 'Page_category';				
$INFO_TABLE = 'PageInfo';						


$sql="SELECT PageInfo.ID, PageInfo.Description FROM PageInfo INNER JOIN Page_category ON PageInfo.Category = Page_category.Category_ID WHERE PageInfo.Category LIKE '%" & $sParent & "%'";
$rs = $conn->Execute($sql);
	
	

	If(!$rs->EOF) {
		echo 'Sorry, there are no pages listed for this category.';
	}
	Else	{
		echo 'Here are the pages for this category.';
	}


?>
<?php
while (!$rs->EOF)
{
    echo $rs["Description"];
    $rs->MoveNext();
}

$rs->Close();
$conn->Close();

$rs = null;
$conn = null;

?>
What connection code do I have to use to connect to my Access database??
I established an ODBC connection with my database called "Accord"

I am a novice at php, so please be patient with me!

Any help would be appreciated.

Posted: Thu Jun 01, 2006 12:43 pm
by Christopher
You might want to look at the ODBC functions.