mysql_num_rows(): supplied argument is not a valid ARGUMENT

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
alme1304
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 7:18 pm

mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by alme1304 »

first of all, thank you for taking the time to read this.
I'm making a small project to manage my projects, but im having trouble with the script that adds a new client to the the database. As of now im getting and error that says

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\testing folder\project_manger\write_client.php on line 15
Could anyone explain to my why this is happening? and why is the code able to bypass the last 'if else' statement?
Thank you for your time. Note: if have highlightened line 15

Code: Select all

<?php
include( 'conn_inc.php' );
include( 'comm_func.php' );
 
#check the fields for user errors
if ($_POST['first_name']=="" || $_POST['last_name'] == ""){
echo "one of the name fields are empty";
exit;
}
$date_logged= (date("Y-m-d")); 
$id=rand(1000,9999);
$sql= "SELECT id FROM client WHERE id='$id'";
 
$mysql_result=mysql_query ($sql,$connect);
[b]$num_rows=mysql_num_rows($mysql_result);[/b]
if ($num_rows!=0){
    $id=rand(1000,9999);
}  
$insert = "INSERT INTO client (first_name,last_name,email,home_phone,cell_phone,office_phone,address,date,id) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[email]','$_POST[home_phone]','$_POST[cell_phone]','$_POST[office_number]','$date_logged','$id')";
if(!$insert){
    echo "the records could to be added at this time. Please try again later";
    exit;
} else {
    echo "the records were added<br />";
    main_menu();
}
 
 
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by califdon »

alme1304 wrote:first of all, thank you for taking the time to read this.
I'm making a small project to manage my projects, but im having trouble with the script that adds a new client to the the database. As of now im getting and error that says

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\testing folder\project_manger\write_client.php on line 15
Could anyone explain to my why this is happening? and why is the code able to bypass the last 'if else' statement?
You have to read these error messages very carefully. Read it again, then notice that that's not what you said in the Subject of your post. What it is telling you is that the supplied argument to the function mysql_num_rows() -- namely $mysql_result -- was not a valid MySql result resource. That's because $mysql_result should be a pointer to a resource, which it would have been if your query was successful. Therefore, it's a direct indication that your query failed. You could have seen what was happening immediately if you followed good practice and made line 14 this way:

Code: Select all

$mysql_result=mysql_query ($sql,$connect) or die(mysql_error());
Then if there is an error executing the query, your script will halt and the error will be echoed to the screen. You should always do this, at least until everything is working properly.
alme1304
Forum Newbie
Posts: 2
Joined: Fri Jan 09, 2009 7:18 pm

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by alme1304 »

hey,
thanks for the response, your answer has solved the issue, but now I for some reason it is not adding the actual data to the db any. Any suggestion? all it does is supply the else statement that I added in case the query was succesful
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by califdon »

alme1304 wrote:hey,
thanks for the response, your answer has solved the issue, but now I for some reason it is not adding the actual data to the db any. Any suggestion? all it does is supply the else statement that I added in case the query was succesful
I see that you formed the SQL string to insert a row into your client table, but I don't see anywhere that you ever executed the query on the database. Nothing happens until you execute a query.
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by wpsd2006 »

This problem usually cause by wrong string sql

i don't know if this work

Code: Select all

$sql= "SELECT id FROM client WHERE id='$id'";
i prefer write it like this

Code: Select all

$sql= "SELECT id FROM client WHERE id=".$id;
if you still get an error
try your sql code in the mysql query or phpmyadmin they will tell you exactly which part is wrong
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by califdon »

It's not a matter of preference. If the id is a text field, quotes are mandatory, if the id is a numeric field, you cannot use quotes. But the problem is that he hasn't executed the query.
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by wpsd2006 »

$mysql_result=mysql_query ($sql,$connect);
$num_rows=mysql_num_rows($mysql_result);

he already use the mysql_query

mysql_num_rows is used after the mysql_query before the fetch_assoc

sometimes it produce error ( no prob in sql string, connect )
but mostly its ok
i recently got this kind of problem

mysql_num_rows count the row result from the given query so if the data its not exist it should return 0 or null
not mysql_num_rows(): supplied argument is not a valid MySQL result resource in

is there a bug on the mysql_num_rows
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: mysql_num_rows(): supplied argument is not a valid ARGUMENT

Post by wpsd2006 »

there isn't a bug to begin with

i solve my problem already..
session stuff
Post Reply