Jump to content

MySQL JOIN and PHP


conker87

Recommended Posts

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:
[code] $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>";[/code]

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

[quote]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[/quote]

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.
Link to comment
https://forums.phpfreaks.com/topic/25066-mysql-join-and-php/
Share on other sites

[quote author=conker87 link=topic=112682.msg457418#msg457418 date=1161787713]
[code] $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);
[/code]
[/quote]
Looks like you want the following for your $numi assignment

[code]
$numi = mysql_num_rows($resu1ti);
[/code]

You can use mysql_fetch_array or mysql_fetch_assoc in the following way to go through results
[code]
$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";
    }

}
[/code]
http://www.php.net/mysql_fetch_assoc
Link to comment
https://forums.phpfreaks.com/topic/25066-mysql-join-and-php/#findComment-114406
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.