Jump to content

mysql_num_rows... not a valid MySQL result resource?


Unholy Prayer

Recommended Posts

Ok, this is my code to get the number of threads in the forum:
[code]
$count_sql = mysql_query('SELECT * FROM threads WHERE $fid = fid');
$threads = mysql_num_rows($count_sql);[/code]

This is the error message that i get:
[quote]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mtechdev/public_html/bbultimate/index.php on line 41[/quote]

Can someone tell me what is wrong with my code?
I don't know how to fix it... this is my entire page of coding:

[code]<?php
include('styles/default/page_header.tpl');
require_once('config.php');

//Let's start the category selection.
$getcats = mysql_query("select * from categories");

while($r=mysql_fetch_array($getcats))
{
 
  $id=$r["id"];
  $name=$r["name"];
  $description=$r["description"];
 
  echo "<table align='center' width='95%' cellspacing='1' cellpadding='1' style='background-color: #000000;'>
        <td align='center' colspan='4' class='bgcat'><p align='left'><b>$name</b>- $description</p></td>
  </tr>
          <tr>
        <td class='cellnames' width='50%'>Forum Name</td>
        <td class='cellnames' width='15%'>Threads</td>
        <td class='cellnames' width='15$'>Replies</td>
        <td class='cellnames' width='20%'>Latest Post</td>
          </tr><tr>";


//Now we display the forums for the selected category.
$getforums = mysql_query("select * from forums where cid = $id order by fid");


while($r=mysql_fetch_array($getforums))
{
 
  $fid=$r["fid"];
  $forumname=$r["forumname"];
  $fdescription=$r["description"];
  $cid=$r["cid"];
 
//How many threads are there in the displayed forum?
$count_sql = mysql_query('SELECT * FROM threads WHERE fid = $fid') or die(mysql_error());
$threads = mysql_num_rows($count_sql);

  echo "<tr>
        <td align='left' class='forumrow' width='50%'><a href='viewforum.php?id=$fid'>$forumname</a><br>$fdescription</td>
        <td class='forumrow' width='15%'>$threads</td>
        <td class='forumrow' width='15%'>0</td>
        <td class='forumrow' width='20%'>Not Available</td>
</tr>";
}
  echo "</table>";
 
}
 


?>[/code]
do this...

//How many threads are there in the displayed forum?
$count_sql = mysql_query('SELECT * FROM threads WHERE fid = $fid') or die(mysql_error());
print("<hr>SQL:$count_sql<hr> "):
#$threads = mysql_num_rows($count_sql);

Add that pring statement and comment out the line that is crashing the program and see what the sql looks like


-John
This line:
[code]$count_sql = mysql_query('SELECT * FROM threads WHERE fid = $fid') or die(mysql_error());[/code]

uses single quotes.  The variable won't be processed in single quotes.  Change it to:
[code]$count_sql = mysql_query("SELECT * FROM threads WHERE fid = $fid") or die(mysql_error());[/code]
what you should REALLY do is this...
[code]
$count_sql = mysql_query("SELECT * FROM threads WHERE fid = '". $fid ."'") or die(mysql_error());
[/code]
take note of the single quotes wrapped around the double quotes wrapped around $fid.
[quote author=Unholy Prayer link=topic=116788.msg476025#msg476025 date=1164849356]
Ok, this is my code to get the number of threads in the forum:
[code]
$count_sql = mysql_query('SELECT * FROM threads WHERE $fid = fid');
$threads = mysql_num_rows($count_sql);[/code]

This is the error message that i get:
[quote]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mtechdev/public_html/bbultimate/index.php on line 41[/quote]

Can someone tell me what is wrong with my code?
[/quote]


Had the same problem yesterday. Try this $count_sql = mysql_query("SELECT * FROM threads WHERE fid = '$fid'") or die(mysql_error());

Change single ' to double ' and the put single ' around $fid like this '$fid'

should work than

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.