Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO即使没有';不绑定任何参数_Php_Mysql_Pdo - Fatal编程技术网

Php PDO即使没有';不绑定任何参数

Php PDO即使没有';不绑定任何参数,php,mysql,pdo,Php,Mysql,Pdo,我试图生成一些异常来测试函数,所以我没有故意将任何数组传递给$stmt->execute(),我希望得到一个异常,或者什么都没有(因为没有匹配的结果),但我仍然能够获取一些内容并将其打印出来 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql='Select id from books where bookname=?'; $stmt=$conn->prepare($sq

我试图生成一些异常来测试函数,所以我没有故意将任何数组传递给
$stmt->execute()
,我希望得到一个异常,或者什么都没有(因为没有匹配的结果),但我仍然能够获取一些内容并将其打印出来

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql='Select id from books where bookname=?';

    $stmt=$conn->prepare($sql);

    $stmt->execute();

    if($row=$stmt->fetch(PDO::FETCH_ASSOC)){

      print_array($row);  //print_array is a function can print out key and the value

    }  
打印出来

[id] => 123 (just an example)
如果我将查询更改为

'Select * from books where bookname= ?';
它打印出表中的第一行数据

我想知道execute()和fetch()之间发生了什么。。。。我直接在phpmyadmin中尝试了这些查询(我可以想象当它们到达DB时查询会是什么样子),但没有得到相同的结果

Select id from books where bookname= ?
Select id from books where bookname= '?'
Select id from books where bookname= ''
Select id from books where bookname= ' '
到目前为止,PDO对我来说充满了惊喜

顺便说一句,我甚至
var\u dump($stmt->execute())
并返回
true

更新:

我打开

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
并尝试打印出
$conn->errorInfo()

$stmt->errorInfo()

没有错误消息。还是刚拿到结果

但是,当我设置
$conn->setAttribute(PDO::ATTR\u EMULATE\u PREPARES,TRUE)时

我得到一个例外:

SQLSTATE[HY093]: Invalid parameter number: no parameters were bound' in ...
如果我关闭它,不管在execute()中是否有数组,它都可以工作

但许多文章希望此属性为false,以防止SQL注入

在我的情况下,设置为false似乎只是为了允许以某种方式进行注射


有什么想法吗?

PDO默认为无提示错误,例如,失败时返回布尔值
false
。除非您显式地打开异常,否则它永远不会抛出任何异常

由于您的代码不检查返回值,因此您永远不会得到任何失败的迹象,例如

$conn->prepare($sql) or die($conn:errorInfo);
我会告诉你的

试一试


启用异常。

您已经执行了您认为正确的其他语句

$conn->prepare($sql); // there is no assign
$stmt->execute();
换成

$stmt = $conn->prepare($sql); // there is assign
$stmt->execute();

我也试过这个。。。它只是打印出一些东西,比如我的代码,实际代码是这样分配的,但仍然生成结果。在我的实际代码中,就像您希望我更改的一样。我为这个问题制作了一个示例代码。提供重现行为所需的真实代码或最短代码。此代码生成完全相同的结果。我刚更改了表名
$stmt = $conn->prepare($sql); // there is assign
$stmt->execute();