php mysql statement not working

倾然丶 夕夏残阳落幕 提交于 2021-02-11 17:20:50

问题


I have the following php mysql statment that is working fine:

if (isset($_GET['name'])) {

        $data = "%".$_GET['name']."%";
        $sql = 'SELECT * FROM tbl_clients WHERE fname like ?';

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

        $results = $stmt->execute(array($data));

        $rows = $stmt->fetchAll();
        $error = $stmt->errorInfo();
    }

But i want to add the following so it can check to columns for the name variable:

$sql = 'SELECT * FROM tbl_clients WHERE fname like ? or lname like ?';

If i modify this statement to the above it errors out with the following:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /var/www/www.test.com/search.php on line 38 

回答1:


In the updated query, you have two parameters, but you're only passing one value to execute. Just fix the latter problem, and it will work:

$results = $stmt->execute(array($data, $data));



回答2:


It's pretty obvious, you have two ? and only one item in the array you are passing to $stmt->execute

$sql = 'SELECT * FROM tbl_clients WHERE fname like :something or lname like :something';

$data = array(':something'=>'SOME VALUE');
$results = $stmt->execute($data);


来源:https://stackoverflow.com/questions/24415897/php-mysql-statement-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!