mysql fetch error?

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
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

mysql fetch error?

Post by mike08 »

Trying to view a guestbook but i keep getting this error which i am struggling to overcome hope you can help. The error is

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\shopbots\view_guest.php on line 13

Code: Select all

<?
//Display the top of the page for the guestbook
echo " <center><h1> Guestbook </h1></center><br>";
echo ' <a href="sign_guestbook.php"> Click here to leave us a message</a><br><br>';
echo " <b>comments from visitors </b><hr>";

//make the query
$query = "SELECT (username,email,comments) FROM users guestbook ORDER_BY user_id";
$result = @mysql_query ($query); //Run the query.

//Fetch and print all the records.
	while ($row = mysql_fetch_array($result)) 
	{
			echo "<b>username:</b> $row[0]<br>email: </b>$row[1]<br>
			<b>Comments:</b> $row[2]";	
	}
	    mysql_close(); // close the database connection
	?>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

try changing this line

Code: Select all

<?php
$result = @mysql_query ($query);
?>
with this

Code: Select all

<?php
$result = mysql_query ($query) or die("ERROR, ERROR!".mysql_error());
?>
and see what error you get
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Post by mike08 »

i get this error - i did solve another error as i had users and guestbook in before but taken users out which was wrong but i now get this error

Code: Select all

ERROR, ERROR!You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'email,comments) FROM guestbook ORDER_BY user_id' at line 1
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

you don't nee parentheses ( "()" ) around the username, email, comments in your query
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Post by mike08 »

still doesn't seem to be working this is my code now

Code: Select all

$query = "SELECT username, email, comments FROM guestbook ORDER_BY user_id";
This is the error
ERROR, ERROR!You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'user_id' at line 1
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

you have both a username and a user_id in your database? If so, I think you made need to select user_id as well in order to order by it
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Post by mike08 »

unfortunately im still getting the same error - very confusing.

:?
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

it should be ORDER BY not ORDER_BY
mike08
Forum Commoner
Posts: 57
Joined: Mon Apr 19, 2004 3:18 am

Post by mike08 »

Thanks - works now :D
Post Reply