Link to home
Start Free TrialLog in
Avatar of gladxml
gladxml

asked on

php script to access mysql

I had the below script that I get from the web but unfortunately the code does not work. The error that I am getting is

Parse error: syntax error, unexpected '}', expecting ',' or ';' in C:\xampp\htdocs\xampp\GTS\test2.php on line 16

Somehow it does not connect to the db. XAMPP is installed on my PC. What are the thing that I need to do or setup so that the below script will run on my PC.

<?
// ADO Connection description
$db = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=gts;UID=root;PWD=; OPTION=35";

// Create connection object
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");

// Open the database
$conn->Open($db);

// Execute the query
$rs = $conn->Execute("SELECT * FROM lktbl_jobcurr_relevance WHERE jobcurr_isDisplayed=1 ORDER BY jobcurr_Order;");
while (!$rs->EOF)
{
echo "test <br/>"
}
$rs->MoveNext()
?>
SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of gamebits
gamebits
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use single quotes in this case:
$db = 'DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=gts;UID=root;PWD=; OPTION=35';

{ in double quotes have special menaings http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

and ; are missing for the lines:

echo "test <br/>";
}
$rs->MoveNext();
?>
An here is also a logical error:

while (!$rs->EOF)
{
echo "test <br/>";
}
$rs->MoveNext();

should be:
while (!$rs->EOF) {
  echo "test <br/>";
  $rs->MoveNext()
}
else you have an ifinitive loop.
Avatar of gladxml
gladxml

ASKER

the code is now like this

<?
// ADO Connection description
$db ='DRIVER={MySQL ODBC 3.51 Driver;}SERVER=localhost;DATABASE=gts;UID=root;PWD=; OPTION=35';

// Create connection object
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");

// Open the database
$conn->Open($db);

// Execute the query
$rs = $conn->Execute("SELECT * FROM lktbl_jobcurr_relevance WHERE jobcurr_isDisplayed=1 ORDER BY jobcurr_Order;");
while (!$rs->EOF)
{
echo "test <br/>";
}
$rs->MoveNext();
?>

and the error that I get now is


Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Service Components<br/><b>Description:</b> Format of the initialization string does not conform to the OLE DB specification.' in C:\xampp\htdocs\xampp\GTS\test2.php:9 Stack trace: #0 C:\xampp\htdocs\xampp\GTS\test2.php(9): com->Open('DRIVER={MySQL O...') #1 {main} thrown in C:\xampp\htdocs\xampp\GTS\test2.php on line 9
Ok this this case the connection string mustlook like:
$db ='DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=gts;UID=root;PWD=; OPTION=35';

as steelseth12 first mentioned in his code.
Avatar of gladxml

ASKER

Thanks but I got it working by downloading the adodb for php then all works below is the working script...

<?
include('adodb/adodb.inc.php');
$DB = NewADOConnection('mysql');
$server = "localhost";
$user= "root";
$pwd = "";
$db = "gts";
$DB->Connect($server, $user, $pwd, $db);

$rs = $DB->Execute("SELECT * FROM lktbl_jobcurr_relevance WHERE jobcurr_isDisplayed=1 ORDER BY jobcurr_Order;");
while (!$rs->EOF)
{
echo "test <br/>";
$rs->MoveNext();
}
?>
Avatar of gladxml

ASKER

But I have anoher problem when I try to write the field values it does not appear. I am currently using the below line to display the field values

echo($rs->Fields['jobcurr_id']->Value);

Is this the correct way to write the field values.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial