本文介绍了php mysql 语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下运行良好的 php mysql 语句:

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

推荐答案

在更新的查询中,您有两个参数,但您只将一个值传递给 execute.只需解决后一个问题,它就会起作用:

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));

这篇关于php mysql 语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:04
查看更多