我有以下php代码:
$id=1;
$query = 'select id from role_perm where perm_id = :id';
if($statement =$GLOBALS["DB"]->prepare($query)){
$result =& $statement->execute(array(':id' => $id));
$t = $result->fetch_all();
但是当我尝试运行此代码时,我得到一个错误:
1064您的SQL语法有错误;请查看手册
对应于MySQL服务器版本,以便使用正确的语法
靠近:第1行的“id”
我在网上浏览了很多问题和信息,试图用不同的方式表达我的观点,但我总是以这样的错误告终,我试过了吗?参数也是。如何使其识别参数?
我的数据库配置:
$databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($databaseConnection->connect_error)
{
die("Database selection failed: " . $databaseConnection->connect_error);
}
// Create tables if needed.
prep_DB_content();
$GLOBALS['DB'] = $databaseConnection ;
最佳答案
$query = 'select id from role_perm where perm_id = ?';
if($statement = $GLOBALS["DB"]->prepare($query)){
$statement->bind_param("i", $id);
$statement->execute();
$statement->store_result();
$statement->bind_result($id_result);
while ($statement->fetch()) {
// code here
}
$statement->close();
}