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

Supplied argument is not a valid MySQL result resource

hi,

i write a code like this

$user_id = $hd_cookie['user_id'];
$user_password = $hd_cookie['auth'];


$sql = "SELECT * FROM hd_users WHERE user_id='$user_id' AND user_password='$user_password'";

$r_user = mysql_query($sql);
$num=mysql_num_rows($r_user); //line 24

it shows Warning
Warning: Supplied argument is not a valid MySQL result resource in c:\apache\htdocs\helpdeskfinal\includes\auth.php on line 24

when i print the sql statement and run the statement in MySql Window it gives the result.but from here it didn't give the result why?

i am using PHPTRIAD 2.1.1 version is this version supports mysql_num_rows????

please solve my problem

Thanks
Srinivas

sri_sep29
Newbie Poster
1 post since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

It might return an error if the result of the SELECT contained no rows, but I could be wrong. The code looks ok, I'm not sure what the problem could be.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

Debugging tip: You mention that you run the statement in MySql window, and it works, but did you actually copy & paste the statement as generated by your code? Right after you build the $sql variable, output it like so:
[php]
echo $sql;
exit();
[/php]
Then copy & paste that output in your MySql query windows and see if it works. You may find your problem.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

I had exactly the same problem and I knew for certain I did spell the cellnames and tablename right.

I've found the solution. Simply put all the cellnames and tablename between ``. That's all!

[php]
$sql = "SELECT * FROM `hd_users` WHERE `user_id`='$user_id' AND `user_password`='$user_password'";
[/php]

If this doesn't work, first get the user_id and user_password out of the ``, only hd_users. If that doesn't work either, I don't it, for me it worked :)

grtz, Jero

Jero
Newbie Poster
1 post since Aug 2005
Reputation Points: 10
Solved Threads: 1
 

Thanks very much, I've just find this via google, and its got me out of a tight spot ;)

Thanks again, and sorry to bring up an old topic :(

manofgames
Newbie Poster
2 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

Thanks, this thread helped me figure out a problem I was having too.

In my case, the problem was a missing semicolon at the end of the SQL statement. Was this also the problem for the original poster, I wonder?

In other words, this:

...snip... AND user_password='$user_password'";


should have been this:

...snip... AND user_password='$user_password';";
gregarious
Newbie Poster
1 post since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

The gotcha for me was remembering to use the mysql_select_db() function. I use mysql_connect() and then mysql_select_db() before I do the mysql_query. Like this...

$sql_conn = mysql_connect($hostName, $dbuser, $dbpass);
mysql_select_db($databaseName, $sql_conn);
$sql = "select * from tablename;";
$rs = mysql_query($sql, $sql_conn);


That did the trick for me.

BrandonJoyce356
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

The error caused by mysql_select_db not connected to the database.
Most of the time because you don't have the privilege to the database or your database in MySQL has no user set for it yet.

Here is how to check:
From your cpanel, go to MySQL Databases.
Check your User for the database that you use if it empty.
If its empty, you need to add from Add User To Database below.
If its not empty, match them with the one you set in your mysql_connect()

sanjayagida
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I dont know if this help you guys, but I fixed my error by removing a # simbol that was in the name of my table

elamigosam
Light Poster
31 posts since Jun 2009
Reputation Points: 10
Solved Threads: 2
 

This error is usually caused by the mysql_query statement querying the wrong connecting string, are you connecting to more than 1 database?

The easiest fix is to implicitly specify the connection string in the SQL Statement

mysql_query($sql,$con);

djmeltdown
Newbie Poster
1 post since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

$q1="SELECT categoryproduct.b_id,category.a_id, category.parentname, categoryproduct.childname, categoryproduct.title FROM category LEFT JOIN categoryproduct ON category.a_id = categoryproduct.a_id";

i hv a use this query in above...is a complettly run on mysql database but not performed in php webpage in my design
and genrate look like as a error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\AdminPanel\menu1.php on line 87

so help me sir plssssssssss

yogesh0720
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You