Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

[web] PHP w/mySQL problem

Started by hughiecoles Nov 13, 2007 at 9:02 PM 8 replies 1.9k views
Original Post
hughiecoles
hughiecoles
ok, i'm having problems connecting to a data base using PHP, i'm using the book "How To Do Everything in PHP and mySQL" and its not working, so i'm wondering if someone can look at hte code and see if i'm messing up somewhere.




<?php

$connection = mysql_connect('localhost','root','password1') or die('Connection Error');


mysql_select_db('hughie') or die('can't Find to database');

$query =  'SELECT * FROM items';

$result = mysql_query($query); or die('No Results');

if(mysql_num_rows($result) > 0)
{
echo '<table width=100% CELLPADDING=0 BORDER=1>';
echo '<tr><td>ID</td><td>NAME:</td><td>PRICE</td></tr>';

while($row = mysql_fetch_row($result))
{
echo '<tr>';
echo '<td>' . &row[0] . '</td>';
echo '<td>' . &row[1] . '</td>';
echo '<td>' . &row[2] . '</td>';
echo '</tr>';

}

echo '</table>';
}

else
{
echo ' No Records Could Be Found';

}

mysql_free_result('$result');

mysql_close($connection);

?>



thanks
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Hodgman
Hodgman
Can you go into more detail as to what "not working" means?

Does it print out "Connection Error" / "can't Find to database" / "No Results" / "No Records Could Be Found", or does it have a different problem?


Also, the problem could be in your MySQL database, and not in your PHP code... Have you double-checked that the password to the root account is "password1", that a DB exists named "hughie", and that it contains a table named "items"?

-------------------------------------
-Well then, so long. Dont cry. Or feel too down. ;)
hughiecoles
hughiecoles
it was showing an internal 500 error, now it just shows the BG color but no text, the die() commands don't even show.


yeah i'm sure the mySQL stuff is rgiht because i set the databases up from the command line tool myself. the username i'm not sure about, because when i log in it just asks for my password, but i was reading that the default is 'root'?
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
hughiecoles
hughiecoles
and now its settled on http 500, i haven't changed the code, it just seems that sometimes it shows a blank page, and other times it shows a http 500 error
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
cobru
cobru
check the server log (i presume you're using apache), the error_log file would tell you more about what happened internally... if you still need help, post here what you find in that log...
Q: How many programmers does it take to write a nice piece of software?A: MORE.
hughiecoles
hughiecoles
[Wed Nov 14 20:28:25 2007] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_STRING in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\database.php on line 12
[Wed Nov 14 20:28:53 2007] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_STRING in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\database.php on line 12



there's a problem with this line

mysql_select_db('hughie') or die('can't Find to database');


but i can't see where the probelm is, i'm 100% sure there's a database named hughie

--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Mattman
Mattman
What happens if you change the line to this?

$db = mysql_select_db('hughie', $connection) or die('Can't find to database');
johnhattan
johnhattan
Quote:
Original post by godsenddeath
[Wed Nov 14 20:28:25 2007] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_STRING in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\database.php on line 12
[Wed Nov 14 20:28:53 2007] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_STRING in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\database.php on line 12



there's a problem with this line

mysql_select_db('hughie') or die('can't Find to database');


but i can't see where the probelm is, i'm 100% sure there's a database named hughie
The problem is the quote in "can't". It's closing the string. Escape the quote to to 'can\'t find database' so PHP knows you're wanting a quote character and not a string-close.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually
Hodgman
Hodgman
>>mysql_select_db('hughie') or die('can't Find to database');

Now I see the problem immediately - PHP knows that your error string (can't Find to database) is a string, because it's surrounded by quotes ('), right?
But the string itself contains a quote character!

So PHP thinks you've written:
mysql_select_db('hughie') or die('can' [ERROR HERE->] t Find to database');

Using double-quotes (") should fix this problem:
mysql_select_db('hughie') or die("can't Find to database");

[edit] johnhattan beat me to reply ;)
hughiecoles
hughiecoles
thank you so much, i haven't been this frustrated with a programming problem in a while( i love learning new things, but sometimes the growing pains drive me nuts)
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.