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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.