Jump to content


Photo

MySQL JOIN and PHP


  • Please log in to reply
1 reply to this topic

#1 conker87

conker87
  • Members
  • PipPipPip
  • Advanced Member
  • 504 posts
  • LocationDerby

Posted 25 October 2006 - 02:48 PM

I'm running an article service, and would like comments to be added by people.

Tables as follows:

article
id, title, author, type, content, copyright

comment
id, name, comment, article_id

Here is the code I'm using:
$queryi		=	"SELECT comment.name, comment.comment, comment.article_id, article.id FROM comment INNER JOIN article ON comment.article_id=article.id" or die ("Unable to find data");
	$resulti	=	mysql_query($queryi) or die("Error:<br>". mysql_error(). "<br>With query:<br>". $queryi);
	$numi		=	mysql_fetch_array($resulti); //mysql_numrows($resu1ti);
	
	if	(!($numi	==	0))
		{

	echo "<p>";

		$ii	=	0;
			while ($ii < $numi)
				{
					$name		=	mysql_result($resulti,$ii,"name");
					$comment	=	mysql_result($resulti,$ii,"comment");
						
						echo "<b>$name</b>";
						echo "<p>$comment";
						echo "<br><hr size='1' color='#FF0000'></p>";
						
					$ii++;
				}
	echo "</p>";

This code infinitely loops, showing the test comment I posted, yet also throwing up the following error hundreds of times:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 7 in /home/fr29leag/public_html/article.php on line 97

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 7 in /home/fr29leag/public_html/article.php on line 98


I'm also noticing it's doing this for all articles and not just the article I posted the test comment on.

Anyone know what's wrong? Any help would be appriciated.
WG4G - Articles, Opinions, Podcasts and Reviews.

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 25 October 2006 - 07:38 PM

$queryi		=	"SELECT comment.name, comment.comment, comment.article_id, article.id FROM comment INNER JOIN article ON comment.article_id=article.id" or die ("Unable to find data");
	$resulti	=	mysql_query($queryi) or die("Error:<br>". mysql_error(). "<br>With query:<br>". $queryi);
	$numi		=	mysql_fetch_array($resulti); //mysql_numrows($resu1ti);

Looks like you want the following for your $numi assignment

$numi =	mysql_num_rows($resu1ti);

You can use mysql_fetch_array or mysql_fetch_assoc in the following way to go through results
$query = '..';
$result = mysql_query....
if (mysql_num_rows($result))
{
    while ($row = mysql_fetch_assoc($result))
    {
        echo $row['name']."<br />\n".$row['comment']."<br /><br />\n\n";
    }

}
http://www.php.net/mysql_fetch_assoc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users