当SQL查询返回数据时,PDO返回null对象

时间:2014-11-19 22:18:16

标签: php mysql sql select pdo

当我直接向MYSQL查询我的长查询时,我得到了正确的数据

但是后来我使用相同查询的PDO我没有数据(对象大小为零)。以下数据

SQL声明:

SELECT table1.field1,table2.field1 FROM table1,table2 WHERE table1.key = table2.key AND table2.field2 ='coffee' AND (table2.date BETWEEN '2014-1-1' AND '2014-12-1');

当我直接在mysql上运行查询时,我得到了输出。

然而,当我使用具有以下内容的php文件运行时,我什么都没得到

include 'config.php';
$query = 'SELECT table1.field1,table2.field1 FROM table1,table2 WHERE table1.key = table2.key AND table2.field2 ='coffee' AND (table2.date BETWEEN '2014-1-1' AND '2014-12-1')';
$stmt = $db->query($query)->fetchAll();
var_dump($stmt);

输出

array(0) { } 

我也试过

$query = "SELECT table1.field1,table2.field1 FROM table1,table2 WHERE table1.key = table2.key AND table2.field2 ='coffee' AND (table2.date BETWEEN '2014-1-1' AND '2014-12-1')";

  $query =  ' SELECT table1.field1,table2.field1 FROM table1,table2 WHERE table1.key = table2.key AND table2.field2 = \'coffee\'  AND (table2.date BETWEEN  \'2014-1-1\'  AND  \'2014-12-1\' )' ;

$query =  ' SELECT table1.field1,table2.field1 FROM table1,table2 WHERE table1.key = table2.key AND table2.field2 = \'coffee\'  AND  \( table2.date BETWEEN  \'2014-1-1\'  AND  \'2014-12-1\'\)' ;

$query =  " SELECT table1.field1,table2.field1 FROM table1,table2 WHERE table1.key = table2.key AND table2.field2 = \'coffee\'  AND  \( table2.date BETWEEN  \'2014-1-1\'  AND  \'2014-12-1\'\)" ;

我做错了什么?

0 个答案:

没有答案