简体   繁体   中英

MySQL query produces correct results, but using PDO produces no results - why?

EDIT: I have changed the title of this question, as I am really getting no results, rather than a NULL result.

I am currently working with PDO on a project of mine, and am having a problem I can't seem to get past. A standard MySQL query (executed in phpMyAdmin) returns the results I want, but when I use PDO, it produces no results. I notice if I don't bind the values the query works, so would like to see what I am missing...

Below is the MySQL query:

SELECT AVG( demand_value ) 
FROM Demand
WHERE DATE = STR_TO_DATE(  '06 Nov 2013',  '%d %M %Y' ) 
AND TIME
BETWEEN STR_TO_DATE(  '07:00:00',  '%k:%i:%s' ) 
AND STR_TO_DATE(  '10:00:00',  '%k:%i:%s' );

Below is the PHP code (using PDO) trying to get the same results:

//Assume $date = '06 Nov 2013', $time_from = '07:00:00', $time_to = '10:00:00'
//Those values are parsed in from a custom function
$ave_btwn_times_stmt = 
$connection->get_conn()->prepare("SELECT AVG( demand_value ) 
FROM Demand
WHERE date = STR_TO_DATE(:date, '%d %M %Y')
AND time BETWEEN STR_TO_DATE(:time_from, '%k:%i:%s') 
AND STR_TO_DATE(:time_to, '%k:%i:%s')");

$ave_btwn_times_stmt->bindValue(':date', $date, PDO::PARAM_STR);
$ave_btwn_times_stmt->bindValue(':time_from', $time_from, PDO::PARAM_STR);
$ave_btwn_times_stmt->bindValue(':time_to', $time_to, PDO::PARAM_STR);
$ave_btwn_times_stmt->execute();
$ave_date_times = $ave_btwn_times_stmt->fetchAll();

echo "Average on " . $date . ", between " . $time_from . " and " . $time_to . ": " . $ave_date_times[0][0] . ".";

If I implement the code below, I get the results I am after:

$ave_btwn_times_stmt = 
$connection->get_conn()->prepare("SELECT AVG( demand_value )
FROM Demand 
WHERE date = STR_TO_DATE('$date', '%d %M %Y')
AND time BETWEEN STR_TO_DATE('$from_time', '%k:%i:%s') 
AND STR_TO_DATE('$to_time', '%k:%i:%s')");                                             

$ave_btwn_times_stmt->execute();
$ave_date_times = $ave_btwn_times_stmt->fetchAll();

echo "Average on " . $date . ", between " . $time_from . " and " . $time_to . ": " . $ave_date_times[0][0] . ".";

If anyone can help me out on this one I would be very grateful.

Your MySQL query threw this error

I'm sorry I'm afraid you can't do that.SQLSTATE[42S22]: Column not found: 1054 Unknown column 'TIME' in 'where clause'

I have refactored your query removing BETWEEEN and replacing it with 2 comparison tests.

SELECT AVG( demand_value ) FROM Demand 
       WHERE DATE = STR_TO_DATE( '06 Nov 2013',  '%d %M %Y' ) 
       AND  `time_from` >= STR_TO_DATE( `time_from`,'%k:%i:%s' ) 
       AND  `time_to` >= STR_TO_DATE(`time_to`,'%k:%i:%s' )

Using unnamed placeholders the following code provides required output.

$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
    $ave_btwn_times_stmt= $dbh->prepare("SELECT AVG( demand_value )
    FROM Demand WHERE DATE = STR_TO_DATE( ?,  '%d %M %Y' )  
    AND  `time_from` >= STR_TO_DATE( ?,'%k:%i:%s' ) 
    AND  `time_to` >= STR_TO_DATE(?,'%k:%i:%s' )");                                             
    // Assign parameters
    $ave_btwn_times_stmt->bindParam(1,$date);
    $ave_btwn_times_stmt->bindParam(2,$time_from);
    $ave_btwn_times_stmt->bindParam(3,$time_to);
    $ave_btwn_times_stmt->execute();
    $ave_date_times = $ave_btwn_times_stmt->fetchAll();
    echo "Average on " . $date . ", between " . $time_from . " and " . $time_to . " = " . $ave_date_times[0][0] . ".";
}
catch(PDOException $e) {
    echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", mapSelect.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }
//Close the connection
$dbh = null; 

After testing you should remove the error echo, or Try/Catch block and relying on PHP error handling instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM