trouble converting asp code to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gcapp
Forum Newbie
Posts: 14
Joined: Thu Jun 01, 2006 8:59 am

trouble converting asp code to php

Post 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]
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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 & "%'";
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
Last edited by shiznatix on Thu Jun 01, 2006 9:20 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 */
gcapp
Forum Newbie
Posts: 14
Joined: Thu Jun 01, 2006 8:59 am

Post 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.
Last edited by gcapp on Thu Jun 01, 2006 1:08 pm, edited 2 times in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You might want to look at the ODBC functions.
(#10850)
Post Reply