Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.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 向准备好的语句传递多个参数_Php_Mysql_Prepared Statement - Fatal编程技术网

Php 向准备好的语句传递多个参数

Php 向准备好的语句传递多个参数,php,mysql,prepared-statement,Php,Mysql,Prepared Statement,这是我的服务器端代码: <?php include('DBconnection.php'); $q = ""; $q = $_REQUEST["q"]; function getAlSubjects($searchtext){ $connection = db_connect(); $statement = $connection->prepare('select * from olsubjectmaster whe

这是我的服务器端代码:

<?php
    include('DBconnection.php');

    $q = "";
    $q = $_REQUEST["q"];

    function getAlSubjects($searchtext){
        $connection = db_connect();
        $statement = $connection->prepare('select * from olsubjectmaster where (ifnull(?,"")="" or SubjectID like ? or SubjectID like ? ) ORDER BY SubjectID');

        $statement->bind_param(1,$searchtext,PDO::PARAM_STR, 200);
        $statement->bind_param(2,$searchtext.'%',PDO::PARAM_STR, 200);
        $statement->bind_param(3,'%'.$searchtext.'%',PDO::PARAM_STR, 200);        

        $result=$statement.execute();
        $connection.close();
        $statement.close();
        return $result;
    }

    $value='';

    while($row = getAlSubjects($q)->fetch_assoc()) {
        echo $row["SubjectID"];
    }
?>

是,这是不允许的

$statement->bind_param(2,$searchtext.'%',PDO::PARAM_STR, 200);
$statement->bind_param(3,'%'.$searchtext.'%',PDO::PARAM_STR, 200);
这些操作导致创建新的字符串文本。无法绑定字符串文本。你需要

$param2 = $searchtext.'%';
$param3 = '%'.$searchtext.'%';
$statement->bind_param(2,$param2,PDO::PARAM_STR, 200);
$statement->bind_param(3,$param3,PDO::PARAM_STR, 200);
作为补充说明,由于您正在比较%searchtext%,因此无需查找searchtext%


更新:正如Fred指出的,您似乎在使用PDO,但调用是MySQLIAPI的一部分,而不是PDO。正确的all-in PDO是

正如您所说,我已经修复了它,但它仍然不起作用,它说“致命错误:无法在第15行通过引用在D:\xampp\htdocs\GetSubject.php中传递参数3”。该段中是否有语法错误?“虽然($row=getAlSubjects($q)->fetch_assoc())”我更改了所有3个位置,$param1=$searchtext$param2=$searchtext.“%”$param3='%.$searchtext'%'$语句->绑定参数(1,$param1,PDO::param_STR,200)$语句->绑定参数(2,$param2,PDO::param_STR,200)$语句->绑定参数(3,$param3,PDO::param_STR,200);真的吗?如果param2起作用,显然param3也应该起作用,因为它是相同的模式,你犯了与OP相同的错误;仍然在混合mysql API
bind_param()
不是PDO库函数的一部分。奇怪的是,这怎么会有这么多的投票。你在这里混合了mysql API。在哪一行?我应该删除这个函数并直接写吗?上帝只知道用哪个API连接;你不能那样做。从连接到查询,您需要使用相同的方法。
bind_param(x,$xxx,PDO::param_STR
-PDO不适合mysqli。您需要阅读mysqli_准备语句的手册。您不能只是从不同的库中插入任何代码;您知道这不是蔬菜汤;-)
$param2 = $searchtext.'%';
$param3 = '%'.$searchtext.'%';
$statement->bind_param(2,$param2,PDO::PARAM_STR, 200);
$statement->bind_param(3,$param3,PDO::PARAM_STR, 200);