简体   繁体   中英

PDO query is not working

I am trying to run a simple SELECT query. here is the query

$sql1=$conn->prepare("SELECT q.quoteid, q.customername, q.customersurname, q.timestamp, o.name 
FROM quotes as q, occasions as o 
WHERE q.occasionid=o.occasionid 
AND companyid=1 
AND q.day=:day 
AND q.month=:month 
AND q.year=:year 
AND staffid='-1' 
AND (q.complete != 'W' OR q.complete != 'Y') 
AND q.online=0");
$sql1->execute($exearray);
/* here  $exearray contain following value */
Array ( [:day] => 24 [:month] => 1 [:year] => 2014 ) 

Its not even showing any error. If I pass static value 1 in month it is showing data. I run this query directly on DB its working.

So I think there is no error in query. I am running this query locally on MAMP.

have you tried to parse your month's data to string before passing to the pdo???

$exearray = array(':day'=>24, ':month'=>(string)1, ':year'=>2014);

once i faced this situation and i did this and it worked for me. i always have this problem while i trying to use LIMIT command in mysql. i just parse my value to string or pass it through statically. and it doesn't relevant to your MAMP. i have this problem on LAMP and WAMP too. apparently it's a common problem which it's in PDO libraries.

and for the record don't forget to use intval() while you pass your month's variable directly to your query.

You need to fetch your result, execute only returns a boolean (true or false) whether query execution succeeded or not.

Example:

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);

from: http://php.net/manual/en/pdostatement.fetchall.php

Perhaps doing it the "long-way" with the parameters, and/or changing the query to use a join would help.

Also, the try/catch on PDOException might provide more information.

Lastly, note the $cmd->fetch(PDO::FETCH_ASSOC) invoked for iterating through the recordset.

try {       
    $db = new PDO("mysql:host=localhost;charset=utf8", "root", "root");
    $cmd = $db->prepare("
        SELECT q.quoteid, q.customername, q.customersurname, 
            q.timestamp, o.name 
        FROM quotes q
        LEFT JOIN occasions o on q.occasionid = o.occasionid
        WHERE companyid=1 
        AND q.day = :day 
        AND q.month = :month 
        AND q.year = :year 
        AND staffid = '-1' 
        AND (q.complete != 'W' OR q.complete != 'Y')    
        AND q.online = 0
    ");
    $cmd->bindParam(':day', 24, PDO::PARAM_INT);
    $cmd->bindParam(':month', 1, PDO::PARAM_INT);
    $cmd->bindParam(':year', 2014, PDO::PARAM_INT);
    if ($cmd->execute()) {
        while ($row = $cmd->fetch(PDO::FETCH_ASSOC)) {
            echo $row['quoteid']."|".$row['customername']."|".$row['name']."<br/>";
        }
    } else {
        echo "$cmd->execute() returned false.";
    }
} catch (PDOException $e) { echo $e->getMessage(); return; }

In table month column type was Varchar. I change that to integer and now it is working.

ps I have tried using (string) before variable and even double quotes and single quotes on variable value but it didn't work.

Thank you everyone. :)

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