php 如何将多个参数绑定到 MySQLi 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16612251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:26:25  来源:igfitidea点击:

how to bind multiple parameters to MySQLi query

phpmysqli

提问by AF.P

I have a mysql query, but I can't bind param for it

我有一个 mysql 查询,但我无法为其绑定参数

SELECT users.email,users.handle,userprofile.mobile FROM users,userprofile WHERE users.email =? OR users.handle =? OR userprofile.mobile=?

I've tried below line

我试过下面的线

$query = "SELECT users.email,users.handle,userprofile.mobile FROM users,userprofile WHERE users.email =? OR users.handle =? OR userprofile.mobile=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("sss",$email,$username,$mobile);
if ($stmt->execute()) {
if($stmt->num_rows){
   echo '......';
    }
}

but I've received and error :

但我收到了错误:

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

警告:mysqli_stmt::bind_param():类型定义字符串中的元素数与绑定变量数不匹配

回答by Fabio

This is the correct syntax for binding params in mysqli

这是绑定参数的正确语法 mysqli

$SQL = "SELECT 
              users.email,
              users.handle,
              userprofile.mobile 
        FROM users,userprofile      
        WHERE users.email =? OR users.handle =? OR userprofile.mobile=?";

if ($stmt = $mysqli->prepare($SQL)) {

     $stmt->bind_param("sss", $one,$two,$three);
     $stmt->execute();

     //do stuff
}

回答by Josh Balcitis

Try this...

尝试这个...

$stmt = $dbConn->prepare("SELECT users.email,users.handle,userprofile.mobile FROM users,userprofile WHERE users.email = ? OR users.handle = ? OR userprofile.mobile= ?");
$stmt->bind_param("sss", $email, $handle, $mobile);
$stmt->execute();