问题
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