Unknown warning while updating field - php

What sort of error is this, I mean while updating field if I use '', I am getting this error.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\shadaab\aaction.php on line 87
ON 86-87 I have this query
$result = mysql_query("SELECT name FROM album WHERE name='$name'");
if(mysql_num_rows($result) != 0)

Pretty popular question on SO. It means your query failed, so $result is false.
Add
if (!$result){
echo mysql_error();
}
To see what exactly error was. Most probably you have some illegal characters in $name variable. So, before embedding it into query string, you need to mysql_escape_string() it.
$name= mysql_escape_string($name);
Btw, your code is subject to SQL-injection. So, you'd better use prepared_statements

As the documentation says:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
You should check that $result !== false first.

mysql_query returns false on errors. Replace the first line with this (if you want to know the error):
mysql_query("SELECT name FROM album WHERE name='$name'") or die(mysql_error());

For SELECT statements mysql_query() returns a resource on success and FALSE on failure. Check if FALSE === $result

Related

mysql_num_rows PHP

elseif(isset($_POST['submit']))
{
// Look for their user
$lookuser = mysql_query("SELECT * FROM `users` WHERE username='". mysql_escape_string($_POST['username']) ."'");
// If we find a row
if(mysql_num_rows($lookuser) > 0)
But my else for that, echos: An error has occured. <br> If you are sure you entered your username correctly, please contact an administrator.
I've tried to echo $_POST['username']; all works out fine. I've made sure my user exists, that works out fine.
The PHP error I get:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in password.php on line 23
Php.net says this about the return value of mysql_query:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
If you're getting a boolean, it's because there's a mysql error. Use mysql_error() to print the error, it'll help you diagnose the issue.
You probably have an error in your sql query. To be sure of that, echo after your mysql_query mysql_error().

PHP & MySQL, what's returned when the query yields no rows?

I'm a little confused about something in the PHP interface to MySQL. The documentation for mysql_query (used to execute commands and queries) says this for return values:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
The returned result resource should be passed to mysql_fetch_array(),
and other functions for dealing with result tables, to access the
returned data.
Use mysql_num_rows() to find out how many rows were returned for a
SELECT statement or mysql_affected_rows() to find out how many rows
were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
mysql_query() will also fail and return FALSE if the user does not
have permission to access the table(s) referenced by the query.
I understand that I can call mysql_num_rows to get a count of the returned rows from a query assuming I did a command in the {SELECT, SHOW, DESCRIBE, EXPLAIN} set.
Aside from that though... what happens if a query in that set executes successfully (database wise) but returns no result rows? Does mysql_query return true or false in that case (i.e. is this a failure condition)? What's the best way to check for the "no results" possibility of a successful query using this interface?
That would fall into the case of the first part of the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
A query that returns no result rows will return neither true, nor false, but a resource object.
However, the resource object will have no rows, i.e., mysql_num_rows() will return 0 and the first call to mysql_fetch_* will return FALSE. There are a number of ways that you can detect this situation, but calling mysql_num_rows() is probably one of the easiest.
not sure its best way but i generally use mysql_num_rows to check for the result resource and if
$countt = mysql_num_rows($resource);
if($count>0)
{
//do further
}
like this in this case.
to see what it returns,
use
$result = mysql_query("SELECT * FROM customers WHERE phone = $phoneNum", $link);
$count = mysql_num_rows($result);
echo $count;

mysql_query return TRUE on empty table

$rs = mysql_query("SELECT user, userid FROM House WHERE
userid='$userid'");
This return Resource id #37
The table House is empty, shouldn't it return FALSE?
According to the manual http://php.net/manual/en/function.mysql-query.php
I have always gotten FALSE on error, but not this time.
Can someone explain, thanks!
If there is nothing wrong with your query, then mysql_query() will not return false. An empty result set is not an error.
Calling any of the fetch functions against that result will return false. mysql_num_rows() will return 0.
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or false on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc it returns trueon success or false on error.

php mysql_query returns empty value

I created a directory listing and came across this issue.
What value does mysql_query($query1) return if there is no value?
My script received this message from $result, would it be alright to pass array(0)?
Warning: mysql_fetch_array($result) expects parameter 1 to be resource, array given in
try:
mysql_query($query1) or die(mysql_error());
php.net
For SELECT, SHOW, DESCRIBE, EXPLAIN
and other statements returning
resultset, mysql_query() returns a
resource on success, or FALSE on
error
If it doesn't return FALSE, you can use mysql_num_rows to find out just how many rows were returned.

Mysql_fetch_assoc(): supplied argument is not a valid MySQL result … [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I tried everything I could think of, but I keep on getting this error.
Mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /url/ on line 41
if ( $_POST[submit] == "Submit" )
{
$sql="INSERT INTO table (`content`, `userid`, `ttime`) VALUES
('$_POST[content]', '".$user_id."', '".time()."')";
$res = mysql_query($sql,$link) or die(mysql_error());
/* (line 41 is the following)*/
while($result = mysql_fetch_assoc($res)) {
} }
I tried printing out the error (no error prints out just the warning), I tried changing the query, everything I could think of. The code works just fine - it does the insert on click, everything is fine, just that warning is appearing ._.'
Any ideas?
From php documentation on mysql_query()
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN
and other statements returning
resultset, mysql_query() returns a
resource on success, or FALSE on
error.
For other type of SQL statements,
INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success
or FALSE on error.
The returned result resource should be
passed to mysql_fetch_array(), and
other functions for dealing with
result tables, to access the returned
data.
Use mysql_num_rows() to find out how
many rows were returned for a SELECT
statement or mysql_affected_rows() to
find out how many rows were affected
by a DELETE, INSERT, REPLACE, or
UPDATE statement.
mysql_query() will also fail and
return FALSE if the user does not have
permission to access the table(s)
referenced by the query.
You can't fetch a result from an INSERT query.
You are attempting to obtain a data row from a query that is not a SELECT query. You can only fetch associated arrays from a result data set. An INSERT query just does its thing.

Categories