Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey
what I am trying to do is, retrieve data from a mysql database in specific order, here's my script:

PHP
<?
$aQry = "SELECT * FROM cb_blog_article ORDER BY artid";
$artRes = mysql_query($aQry);
$artNumber = mysql_numrows($artRes);

$i=0;
while ($i < $artNumber) {
$a1 = mysql_result($artRes, $i, "artid");
$a2 = mysql_result($artRes, $i, "name");
$a3 = mysql_result($artRes, $i, "title");
$a4 = mysql_result($artRes, $i, "article");
$a5 = mysql_result($artRes, $i, "date_posted");

echo "<div id = 'test'> $a1. $a2. $a3. $a4. $a5</div>";
$i++;

$cQry = "SELECT * FROM cb_blog_comments WHERE artid = $a1 ORDER BY comid";
$cRes = mysql_query($cQry);
$cNumber = mysql_numrows($cRes);


$j=0;
while ($j < $cNumber) {
$c1 = mysql_result($cRes, $i, "comid");
$c2 = mysql_result($cRes, $i, "username");
$c3 = mysql_result($cRes, $i, "email");
$c4 = mysql_result($cRes, $i, "date_posted");
$c5 = mysql_result($cRes, $i, "comment");

echo "<div id = 'test2'> $c1. $c2. $c3. $c4. $c5 </div>";
$j++;
}
}

?>


It's supposed to do the following:
Echo article from articleid
Echo comment(s) from articleid
Echo next articleid
Echo comment(s) from articleid

Now to the problems:

[img]http://i.imgur.com/ADTJE.png[/img]
There are 2 comments on the db for articleid 1, it displays the 2nd comment twice and leaves out the first.

There is 1 comment for articleid 31, doesn't display anything.
Error in picture.

I can't seem to find the problem in my code, so I am helpful for anything.

Thanks
Posted

In the second while loop you use $i as the parameter for mysql_result() where you should use $j
PHP
while ($j < $cNumber) {
$c1 = mysql_result($cRes, $j, "comid");
$c2 = mysql_result($cRes, $j, "username");
$c3 = mysql_result($cRes, $j, "email");
$c4 = mysql_result($cRes, $j, "date_posted");
$c5 = mysql_result($cRes, $j, "comment");

echo "<div id = 'test2'> $c1. $c2. $c3. $c4. $c5 </div>";
$j++;
}

Using more descriptive variable names than $i and $j makes it easier to avoid this kind of errors.
 
Share this answer
 
v2
Why can't you use like this.

PHP
$aQry = "SELECT * FROM cb_blog_article ORDER BY artid";
$artRes = mysql_query($aQry);
while ($row_artRes = mysql_fetch_assoc($artRes)) {
$a1 = $row_artRes['artid'];
$a2 = $row_artRes['name'];
$a3 = $row_artRes['title'];
$a4 = $row_artRes['article'];
$a5 = $row_artRes['date_posted'];
 
echo "<div id='test'> $a1. $a2. $a3. $a4. $a5</div>";
 
$cQry = "SELECT * FROM cb_blog_comments WHERE artid = $a1 ORDER BY comid";
$cRes = mysql_query($cQry);
while ($row_cRes = mysql_fetch_assoc($cRes)) {
$c1 = $row_cRes['comid'];
$c2 = $row_cRes['username'];
$c3 = $row_cRes['email'];
$c4 = $row_cRes['date_posted'];
$c5 = $row_cRes['comment'];
 
echo "<div id='test2'> $c1. $c2. $c3. $c4. $c5 </div>";
}
}
 
?>


don't forget to add mysql_select_db($database, $connection);
before the second recordset.

Thanks

Anand Ayyappan
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900