Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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` ASCmysql_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.

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

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.
/snipif 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

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

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

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

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

![]() |
Want good Registration lo...
|
My PHP scripts constantly...
|

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