mysql_query return TRUE on empty table - php

$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.

Related

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;

Unknown warning while updating field

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

What does a sql-query in php on an empty table return?

I have the following PHP script :
<?php
$all_threads=mysql_query("SELECT * FROM forum_threads WHERE category=$_GET[id]");
if($all_threads){ ?>
//Do something.
<?php
}
else { ?>
//Do something else
<?php
} ?>
The table forum_threads is empty, so the query should return 'false' according to the documentation at http://php.net/manual/en/function.mysql-query.php and the 'else' block should get executed. However, strangely, the if-block is getting executed. How come?
It will return a MySQL resource, not FALSE on 0 rows. It will return FALSE on a query error.
You can check mysql_num_rows() instead. It will be 0 if there were no rows returned.
You also have a SQL injection vulnerabilities. Escape the GET param with mysql_real_escape_string().
If you can, just ditch mysql_*() and use PDO.
It won't return false because there is no error in your SQL statement, it's simply returning an empty result set because there are no matching records.

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.

PHP MySQL INSERT return value with one query execution

Is there anything returned from MySQL/PHP on a INSERT query being executed? Here is my function which I have in a CLASS.
function mysqlQuery($query) {
// Gets the results from the query
$results = mysql_query($query, $this->connection);
// Loops through the queried results as an multi-dimensional array
while($rows = mysql_fetch_array($results, MYSQL_ASSOC)) {
// Push results to single array
$rs[] = $rows;
}
// Return results as array
return $rs;
}
This is how I call the function
$rs = $dbh->mysqlQuery($query);
But executing a INSERT query the $rs returns nothing. Does my function need help or is this the default behavior? Any tips would be helpful as well.
INSERT just returns true or false. to actually return something useful, you will need a SELECT or similar query. there is no result to fetch with INSERT.
From the php documentation:
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.
Although this is a very old thread, it is still relevant. And to those who stumble upon this (like I did), don't give up! There are options. In fact, see this answer on SO
Also, for sqli and pdo, see this
Essentially, an insert statement followed by one of the functions listed in those answers will give you the ID of the last record. The function is used like this:
$LastID = mysql_insert_id();
after the INSERT statement.
From php.net http://us2.php.net/mysql_query
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.
Generally, when devs are building home functions to manipulates queries, they use two methods. One, "query", used for SELECTs and co, and one, "exec", other INSERTs, UPDATEs and others (like in PDO extension).
If you want to keep your function, add a IF statement around the loop checking the type of $results (with a is_resource() for example)
In the example below, I add to my insert clause the "returning" along with the primary key of my table, then after the execute, I do a fetch getting an array with the value of the last inserted id.
<?php
public function insert($employee){
$sqlQuery = "INSERT INTO employee(user_id,name,address,city) VALUES(:user_id,:name,:address,:city) RETURNING employee_id";
$statement = $this->prepare($sqlQuery);
$a ="2002-03-11 12:01AM" ;
$statement->bindParam(":user_id", $employee->getUserId(), PDO::PARAM_INT);
$statement->bindParam(":name", $employee->getName(), PDO::PARAM_STR);
$statement->bindParam(":address", $employee->getAddress(), PDO::PARAM_STR);
$statement->bindParam(":city", $employee->getCity(), PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result["employee_id"];
}
?>
Source

Categories