Jump to content

mysql_num_rows(): supplied argument is not valid


elias

Recommended Posts

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:

 

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

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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 :-)

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.