Computing.Net > Forums > Web Development > Error Running MySQL Query With PHP

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Error Running MySQL Query With PHP

Reply to Message Icon

Name: NarrowPathPilgrim
Date: October 4, 2004 at 15:51:32 Pacific
OS: XP Home
CPU/Ram: 2.52 GH'z 512 Megs Of Ram
Comment:

Hello, I am trying to learn PHP and I tried following code and got this error "Parse error: parse error, unexpected '`' in (PathToPHPFile) on line 22"

Here is the code that I used

<?php
$dbuser = "mysql_root";
$dbpass = "mysql_mypass";
$dbhost = "localhost";
$db = "db_test";
?>

<?php
mysql_connect($localhost, $root, $mypass);
?>

<?php
mysql_select_db($test);
?>

<?php

SELECT `test`.`username` , `test`.`password`
FROM test
ORDER BY `test`.`username` ASC

mysql_close();
?>


This line is the line that caused the error (Line 22)
SELECT `test`.`username` , `test`.`password`

I have tried the query with PHPMyAdmin and it worked, but it don't work with this test PHP file that I made.



Sponsored Link
Ads by Google

Response Number 1
Name: Laler
Date: October 4, 2004 at 16:52:38 Pacific
Reply:

try this one?

<?php
// connection, database info
// and do the connect

$dbuser = "mysql_root";
$dbpass = "mysql_mypass";
$dbhost = "localhost";
$dbname = "db_test";

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error ());
?>


<?php
// select database, using the $connect link
// identifier

mysql_select_db($dbname, $connect) or die (mysql_error ());

// the query
$sql_test = mysql_query ("
SELECT `username`, `password`
FROM `test`
ORDER BY `username` ASC
") or die (mysql_error ());

// fetch the result
$test = mysql_fetch_assoc ($sql_test);
mysql_close();

// if want to test the result
do{

echo $test['username'].' - '.$test['password'].'
';

}while ($test = mysql_fetch_assoc ($sql_test));
?>

if something goes wrong, please post back here, don't forget the error code(s) :)

^o^
are you in Asia? do you watch Animax Asia? Please Vote


0

Response Number 2
Name: Laler
Date: October 4, 2004 at 17:06:41 Pacific
Reply:

better test result:

// if want to test the result
do{

echo $test['username'].' - '.$test['password'].'
<br>';

}while ($test = mysql_fetch_assoc ($sql_test));


/snip
I have tried the query with PHPMyAdmin and it worked, but it don't work with this test PHP file that I made.
/snip

if you put SQL query into phpmyadmin's SQL window, it will be converted first to the correct PHP syntax...

^o^
are you in Asia? do you watch Animax Asia? Please Vote


0

Response Number 3
Name: NarrowPathPilgrim
Date: October 4, 2004 at 18:01:53 Pacific
Reply:

I tried that code and it said "No Database Selected"


0

Response Number 4
Name: Laler
Date: October 4, 2004 at 18:18:10 Pacific
Reply:

ok,

as it said, no database selected... so double check your database name...

mysql_select_db($dbname, $connect)

the above line will select the database $dbname, using $connect resource link identifier...

so you need to make sure that the value of $dbname is correct... the declaration is on the first few lines...

---

if I look at your codes:

$db = "db_test";

the database name is: db_test

or

mysql_select_db($test);

the database name is $test ? does it have value assigned? maybe on previous codes?


---

maybe you named your database as "test" ?


if so, try changing the $dbname on my code above into:

$dbname = "test";


keep us posted


^o^
are you in Asia? do you watch Animax Asia? Please Vote


0

Response Number 5
Name: Laler
Date: October 4, 2004 at 18:24:27 Pacific
Reply:

better mysql_query syntax (specify the resource link identifier):

// the query
$sql_test = mysql_query ("
SELECT `username`, `password`
FROM `test`
ORDER BY `username` ASC
", $connect) or die (mysql_error ());

^o^
are you in Asia? do you watch Animax Asia? Please Vote


0

Related Posts

See More



Response Number 6
Name: NarrowPathPilgrim
Date: October 4, 2004 at 18:44:34 Pacific
Reply:

Thank You!
I got it working
I changed
mysql_select_db($test, $connect) or die (mysql_error ());
To
mysql_select_db(test, $connect) or die (mysql_error ());

Thanks Again
Zach Doty


0

Response Number 7
Name: Laler
Date: October 5, 2004 at 04:39:43 Pacific
Reply:

np :p

the "or die" lines are very good on development stage... when error encountered, it'll terminate the script and output some message...

I got used to put or die mysql_error () on every mysql queries


---

your working code:

mysql_select_db(test, $connect) or die (mysql_error ());

is actually still not done 'by the book'... if you have error notice turned on (also good on development stage), I think it'll tell you something about the database name which isn't enclosed right...

this is the correct one:

mysql_select_db("test", $connect) or die (mysql_error ());

the script might be working, but if you're not doing it by the book & carefully, it may have bugs & holes everywhere :D

^o^
are you in Asia? do you watch Animax Asia? Please Vote


0

Sponsored Link
Ads by Google
Reply to Message Icon

Want good Registration lo... My PHP scripts constantly...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Error Running MySQL Query With PHP

Querying a row from MySQL database with PHP www.computing.net/answers/webdevel/querying-a-row-from-mysql-database-with-php/4094.html

mail merge with php and mysql? www.computing.net/answers/webdevel/mail-merge-with-php-and-mysql/3975.html

Cannot connect to MySQL DB with PHP www.computing.net/answers/webdevel/cannot-connect-to-mysql-db-with-php/4085.html