Jump to content


Photo

mysql_num_rows(): supplied argument is not valid


  • Please log in to reply
3 replies to this topic

#1 elias

elias
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationSpain

Posted 27 November 2005 - 11:55 AM

Hi guys

I am absolutely tearing my hair out about this one. I have done checks and double checks but cant fathom it out - I got to the point that I think there is a problem with the PHP installation at my server - but I have come to learn that something very insignificant can make the code falter and the usual response is "blame the tools".

I am creating a new website and have just created a very simple one-liner database.

I just want to access this database, read the row off my one table and come back and report that everything is ok.

The row has 10 entries and through my cPanel phpMyAdmin I can access and perform the query:

SELECT * FROM general WHERE `header` LIKE 'header'

to get a the row details returned to me. (I even tried misstyping "header" to get a "MySQL returned an empty result set"

So, so far so good. I seem to have created the table correctly and a row of data that I can access from the cPanel Admin tool.

The problem starts when I try to access and retrieve data from my website - this is my code:

mysql_connect ("localhost", "user", "*********") 
         or die ('I cannot connect to the database because: ' . mysql_error()); 
mysql_select_db ("mydatabase");
print  "You are connected to a mySQL database!";
$result = mysql_query("SELECT * FROM 'general' WHERE `header` LIKE 'header'");
$numrows = mysql_num_rows($result);
print  "There are number of  rows: ".$numrows;

The result is very depressing:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
You are connected to a mySQL database!
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mydomain/public_html/indexphp2.php on line 21
There are number of rows:
[/quote]

So as you can see, it keeps on saying that the argument supplied to mysql_num_rows() is not valid...

What am I doing wrong - clearly the fact that it prints the first line, it (seems) to mean that it is connecting to the database, but it cant peform the query...

Could there be a problem with my server, or the database or the table - what do people suggest I try next.

Thanks for yoru time.

Elias





#2 sqlmc

sqlmc
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 November 2005 - 03:20 PM

I would try removing the single quotes from the table and attribute names. Also your using a LIKE statement without a wildcard. So try something like this.


$result = mysql_query("SELECT *  FROM general  WHERE header LIKE '%header%' " );


In this example you should get results from your talbe where the attribute header contains a value with the string 'header' within it.

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 27 November 2005 - 06:31 PM

just compare the difference between the two queries you've executed:
SELECT * FROM general WHERE `header` LIKE 'header' //succeeded in phpMyAdmin

SELECT * FROM 'general' WHERE `header` LIKE 'header' //failed in php code

as you can see, you've put single quotes around general.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 elias

elias
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationSpain

Posted 27 November 2005 - 08:12 PM

HI guys

Thanks for your replies...

IT WORKS!!!!

... but not due to quotes... I tried every combination of quotes, sigle, double, normal an funny accent ones... but nothing...

For the record and for future reference, the problem was a simple administrative coq-up!

In my cPanel administration area, I created the database, then I created the table, then I created the user... and I thought that was it... but later on I realised I had to **Add User to Db**!!

This solved the problem and as expected, the result now is:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]You are connected to a mySQL database!There are number of rows: 2[/quote]

I am so pleased I can continue my project.

Thanks again for reading and keep up the good work.


Elias

... I'll (probably) be back :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users