I am running a query in php using mysql_query()
function, but it is returning zero rows, whereas when I run same query in phpMyAdmin I am getting one row. Don't know what is making things wrong. I have made a same table structure on sqlfiddle with the query
SELECT * FROM cart_discount WHERE email_counter < 1 AND cart_time BETWEEN '2013-01-31 00:00:00' AND '2013-02-01 23:59:59'
http://sqlfiddle.com/#!2/cceb7/4
Please guide where php is making trouble..
This is how query is generated and executed.
$date1 = date('Y-m-d 00:00:00', strtotime(' -1 day'));
$date2 = date("Y-m-d 23:59:59");
$query = "SELECT *
FROM cart_discount
WHERE email_counter < 1
AND cart_time >= '$date1'
AND cart_time <= '$date2'";
$ssql = mysql_query($query, $con) or die('Problem running query: ' . mysql_error());
if (mysql_num_rows($ssql) > 0) {
//do something
} else {
echo 'No rows found';
}
Try to print these variable first $date1
& $date2
before executing it using mysql_query
. see what it is printing if there is some format mismatch with the database then change it. perhaps this might be the only reason. Apart from this Suggestion: There are lots of things you can do to debug your problem. print the actual query what you are executing. copy it and execute it in phpmyadmin.compare the differences.
As a reference from http://php.net/manual/en/function.mysql-fetch-array.php mysql_fetch_array returns an array that corresponds to the fetched row and moves the internal data pointer ahead. So please make sure you are not using this before mysql_num-rows.
If you use mysql_unbuffered_query(), mysql_num_rows() will not return the correct value until all the rows in the result set have been retrieved.
Warning :: mysql_num_rows()
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_stmt_num_rows()
PDOStatement::rowCount()
Using SQL_CALC_FOUND_ROWS and FOUND_ROWS( ) will NOT trigger a race condition on MySQL, as that would pretty much defy their entire purpose.
The results for their usage is actually unique per connection session as it is impossible for processes to share anything in PHP. As far as PHP is concerned, each request represents a new connection to MySQL as each request is isolated to its own process.
To simulate this, create the following script:
<?php
$Handle = mysql_connect( "localhost" , "root" , "" );
mysql_select_db( "lls" );
if( isset( $_GET[ 'Sleep' ] ) ) {
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `bid` From `blocks` Limit 1" );
} else {
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `aid` From `access` Limit 1" );
}
if( isset( $_GET[ 'Sleep' ] ) ) {
sleep( 10 ); // Simulate another HTTP request coming in.
$Result = mysql_query( "SELECT FOUND_ROWS( )" );
print_r( mysql_fetch_array( $Result ) );
}
mysql_close( );
?>
Set the connection and query information for something that matches your environment.
Run the script once with the Sleep query string and once again without it. Its important to run them both at the same time. Use Apache ab or something similar, or even easier, just open two browser tabs. For example:
http://localhost/Script.php?Sleep=10
http://localhost/Script.php
If a race condition existed, the results of the first instance of the script would equal the results of the second instance of the script.
For example, the second instance of the script will execute the following SQL query:
<?php
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `aid` From `access` Limit 1" );
?>
This happens while the first instance of the script is sleeping. If a race condition existed, when the first instance of the script wakes up, the result of the FOUND_ROWS( ) it executes should be the number of rows in the SQL query the second instance of the script executed.
But when you run them, this is not the case. The first instance of the script returns the number of rows of its OWN query, which is:
<?php
mysql_query( "SELECT SQL_CALC_FOUND_ROWS `bid` From `blocks` Limit 1" );
?>
So it turns out NO race condition exists, and every solution presented to combat this "issue" are pretty much not needed.
Good Luck,