Query...Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Query...Query

Post by ericburnard »

Hi there. i have a code which developed an error, and i looked at an earlier post which had the same problem, but now o have tried what was in there i just have a different error.
this is my code

Code: Select all

<?php
// Connecting, selecting database
mysql_connect('localhost', 'madashat_eric', 'gateway2')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('madashat_blog') or die('Could not select database');

echo "<BR>";

$blogid = $_GET['blogid'];

$query = "select * from comments order by id desc where blogID = '$blogid'";
$result = mysql_query($query) or die(mysql_error().'<br />'.$query); 

$number_of_rows = mysql_num_rows($result); 
for ($i = 0; $i < $number_of_rows; ++$i) 
{ 

$id = mysql_result($result,$i,"id");
$name = mysql_result($result,$i,"name");
$date = mysql_result($result,$i,"date");
$time = mysql_result($result,$i,"time");
$comment = mysql_result($result,$i,"comment");

echo "<h2>".$date." :: ".$time."</h2>
<h1>".$name."</h1>
<BR>".$comment."<br>";

}
?>
the error says its to do with the query although i dont think therre is anything wrong with it. (http://www.madashatters.com/bcomments.php?blogid=37)

would be greatfull for advise

Eric
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

your query is bass ackwards.

Code: Select all

$query = "select * from comments order by id desc where blogID = '$blogid'";
should be

Code: Select all

$query = "select * from comments where blogID = '$blogid' order by id desc";
order by should happen *after* the where.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post by ericburnard »

AHHHHHHH thanks very much thats made a whole lot of stuff come together now.

Thanks :D

Eric
Post Reply