我有一个从我的数据库中选择的功能,但是当$id包含引号时它会中断。这是我的代码:
public function database_select_one($table, $identifier, $id) {
$sql = 'SELECT * FROM '.$table.' WHERE '.$identifier.'="'.$id.'" LIMIT 1';
$mysqli_result = $this->query($sql);
$data = $this->fetch($mysqli_result, 1);
return $data;
}
您可以使用
str_replace
$id字段中删除引号。 http://tr2.php.net/str_replace
public function database_select_one($table, $identifier, $id) {
str_replace('"', "", $id);
$sql = 'SELECT * FROM '.$table.' WHERE '.$identifier.'="'.$id.'" LIMIT 1';
$mysqli_result = $this->query($sql);
$data = $this->fetch($mysqli_result, 1);
return $data;
}
use prepare 语句:
$stmt= mysqli_prepare($link, "SELECT * FROM ".$table." WHERE ". $identifier . " = :id LIMIT 1");
mysqli_stmt_bind_param($stmt, "id", $id);
mysqli_stmt_execute($stmt);