954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

[php]
<?php
/* declar some relevant variables */
$Host = "localhost"; //location of mySQL on server
$User = "michael"; //my username
$Pass = "koolaide"; //my password
$Name = "phpaccess"; //name of the database to be used
$Table = "info"; //name of the table within the database

mysql_connect ($Host, $User, $Pass, $Name, $Table) or die ("unable to connect to database");
mysql_select_db("$Name") or die ("unable to select DB");
$sqlquery = "SELECT * FROM $table WHERE opinion = 'is greate'";
$result = mysql_query($sqlquery);
$number = mysql_num_rows($result); //LINE NUMBER 23

$i = 0;
if ($number < 1)
{
echo "There were no results for your search


";
}
else
{
while ($number > $i)
{
$thename = mysql_result ($result, $i, "name");
$theemail = mysql_result ($result, $i, "email");
echo "Name:


$thename
E-Mail:$theemail";
$i++;
}
}
?>[/php]

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\www\databaseconnect.php on line 23
i get that error, ive commented line number twentythree. this is just a database tutorial that im working on and i hope that maybe someone can tell me whats goin on.

o and dont worry about the username password stuff, this server will never go online, this is all practice stuff.

(line twenty three is in reference to the entire php file, this is only part of it, i left out the tags part)

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

You defined the variable "$Table" with a capital T, yet you try to interpolate it in the querystring with a lower case "t." PHP is case sensitive for user defined variables.

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

wow thanks that fixed it, but im not sure if i got the output i am supposed to get. though i plan on working on this a little more. thanks for the help i didnt even notice that. maybe from now i should just try to work with my variables in lowercase.

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

That's usually best. Saves a lot of hassle.

Roberdin
Supreme Evil Overlord
Team Colleague
282 posts since Feb 2003
Reputation Points: 63
Solved Threads: 6
 

now im very happy! i have a submit form which writes to a database, a submitted form which displays what has just been submitted, and a database forms which displays all information that has been submitted.

i plan on posting it in the snipits area.

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 
wow thanks that fixed it, but im not sure if i got the output i am supposed to get. though i plan on working on this a little more. thanks for the help i didnt even notice that. maybe from now i should just try to work with my variables in lowercase.

Glad you were able to get it working! Yeah, I usually keep my variables lower case. The only place I use capital letters are in classes.i plan on posting it in the snipits area.Yes, definitely. I'm sure people will find it helpful! :cool:

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

I am having the same problem
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Abyss Web Server\htdocs\getrecord.inc on line 6
how ever this is my code


<?php
// version 1.1
$qstr = "SELECT * from members where id = '1' ";
$result = mysql_query($qstr);

if (mysql_num_rows($result)) // check login info is correct
{
$username = mysql_result($result,0, "username");
$password = mysql_result($result,0, "password");
echo "The username: $username
";
echo "The password: $password
";
}
else echo "ERROR - unable to find current username and password!";
mysql_close();
?>

please someone help me

civuk
Newbie Poster
1 post since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

I have this problem and i am new so please go slow...

error on page

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/loyalist/public_html/shop/pages/0.php on line 17

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/loyalist/public_html/shop/pages/0.php on line 13 -------- <?php // // TYPES: // // CDS DVDS CLOTHING KEYRINGS/BOTTLEOPENERS FLAGS OFFERS TICKETS // // 1 2 3 4 5 6 7 // $query = "SELECT * FROM config WHERE id=3"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $query = "SELECT * FROM items WHERE id=".$row['value']." ORDER BY id DESC Limit 0,1"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $id = $row['id']; $name = $row['name']; $desc = $row['description']; $price = $row['price']; $img = $row['img']; $menu = $row['menu']; $frase = $desc; $char = 180; $desc = checklenght($frase, $char); $content = $namet.$name.$desct.$desc.$pricet.'£'.$price; ?> item_enlarge.gif

<?php print $img; ?> <?php print $content; ?> item_order1.gifitem_order2.gif
<?php } } ?> Welcome...

<?php $query = "SELECT * FROM config WHERE id=1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $introtext = $row['value']; $introtext = nl2br($introtext); print $introtext; ?> hope someone can help lexi

lexid2002
Newbie Poster
1 post since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

I am having the same problem Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Abyss Web Server\htdocs\getrecord.inc on line 6 how ever this is my code

<?php // version 1.1 $qstr = "SELECT * from members where id = '1' "; $result = mysql_query($qstr);

if (mysql_num_rows($result)) // check login info is correct { $username = mysql_result($result,0, "username"); $password = mysql_result($result,0, "password"); echo "The username: $username
"; echo "The password: $password
"; } else echo "ERROR - unable to find current username and password!"; mysql_close(); ?>

please someone help me

you should check the id field type is it int or varchar ???

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 

For those who got a error with the num rows, I suggest you add the error dispaly for the sql error message.

if(!$result){die(mysql_error();}

Stick that below the query, it will give a specific message about what is wrong with the query. There isn't a problem with the num_rows function usually, but with the actual query itsself.

WhiteLeo
Newbie Poster
16 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
 

Recap on above Post.

If your having an error with mysq_num_rows($res).
Basically you need to verify that your query worked ok.
Code for having error checking setup is.

$ret = mysql_query($query) or die(mysql_error());


This will output the error that is being caused at the query stage.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Same applies to Lexid2002.
you're getting an error on fetch_assoc(), as theres a problem at the query stage.
This can be identified by doing the above code.. adding ' or die(mysql_error()); to the end of the line.

Once you have sorted the query, the mysql_fetch_assoc, will have a valid resource, and you'll not get this error.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

I always forget about the "or die..." thing.

Hopefully it should point out whether it was just a simple sql or connection problem.

WhiteLeo
Newbie Poster
16 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
 

Adding or die(mysql_error()) after a query is a poor way to back out. It would be better to call a function to handle the error than to simply kill the page (possibly even loading a separate error page and/or an automated error reporting system). Besides, do you want someone to see what table names, column names, etc.. you were using in your query?

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

Wow, wow Infarction calm down.
You're jumping the gun a little for these guys.
They're not ready for advanced error handling techniques.
They're just getting to grips with mySQL techniques.
Need to give them a chance to understand how things work, before you can introduce new techniques to them.

Keep plodding along guys, it will make more sense in time. ;)

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

I have this problem and i am new so please go slow... error on page Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/loyalist/public_html/shop/pages/0.php on line 17

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/loyalist/public_html/shop/pages/0.php on line 13 -------- <?php // // TYPES: // // CDS DVDS CLOTHING KEYRINGS/BOTTLEOPENERS FLAGS OFFERS TICKETS // // 1 2 3 4 5 6 7 // $query = "SELECT * FROM config WHERE id=3"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $query = "SELECT * FROM items WHERE id=".$row['value']." ORDER BY id DESC Limit 0,1"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $id = $row['id']; $name = $row['name']; $desc = $row['description']; $price = $row['price']; $img = $row['img']; $menu = $row['menu']; $frase = $desc; $char = 180; $desc = checklenght($frase, $char); $content = $namet.$name.$desct.$desc.$pricet.'£'.$price; ?> item_enlarge.gif

<?php print $img; ?> <?php print $content; ?> item_order1.gifitem_order2.gif
<?php } } ?> Welcome...

<?php $query = "SELECT * FROM config WHERE id=1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $introtext = $row['value']; $introtext = nl2br($introtext); print $introtext; ?> hope someone can help lexi

You forgot to setup a connection??? hehehe

omick16
Newbie Poster
7 posts since Oct 2005
Reputation Points: 13
Solved Threads: 0
 
dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 










[/html][php]<?php
$my_body1 = "";
$my_body1 .= "Ordered on ".date("F j, Y, g:i a") ."\n"."
"."
";
for ($i =0; $i< $HTTP_POST_VARS['orderedtotal'];$i++){if ($xn[$i] != "" )$my_body31 .= "Product ".$xn[$i] . " ".$xd[$i]." @ cost ". $xc[$i]. " with ".$xq[$i] . " ordered"."\n
";}
print $my_body1. str_replace( "\\\"", "\"",$my_body31);
?>[/php][html]

Thank you for your order!
We will be contacting you to confirm.
In five seconds you will be redirected to the main page.
If not then Click Here


http://www.lajdesignsw.com">wcreated.jpg  



acaradic@hotmai
Newbie Poster
3 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

try doing this dude

[PHP]
$Bname = $HTTP_POST_VARS['BName'] ;
$BStreet = $HTTP_POST_VARS['BStreet'];
$sql = "SELECT `id` FROM `customers` WHERE bname='
$Bname' and baddress='$BStreet'".;[/PHP]

w_3rabi
Junior Poster
160 posts since Dec 2006
Reputation Points: 18
Solved Threads: 9
 
try doing this dude [php] $Bname = $HTTP_POST_VARS['BName'] ; $BStreet = $HTTP_POST_VARS['BStreet']; $sql = "SELECT `id` FROM `customers` WHERE bname=' $Bname' and baddress='$BStreet'".;[/php]



i have done what u suggest and now i'm havin this error:Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 3 in /path 2/process.php on line 36

and also out of all images i have had in shopping cart they all come up in mysql as one same record repeated 5 times, for instance i have 5 records of image 02.jpg instead of image 02.jpg, image 03.jpg and so on and none of the records inside customers table.

acaradic@hotmai
Newbie Poster
3 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You