我正在使用以下代码从我的数据库中获取与用户输入的年份和月份相匹配的数据。
连接:
class ConnectDB{
private $servername;
private $username;
private $password;
private $dbname;
protected function connect(){
$this->servername ="localhost";
$this->username ="root";
$this->password ="";
$this->dbname ="dbexpense";
$conn = new mysqli($this->servername,$this->username,$this->password,$this->dbname);
if($conn -> connect_error) {
die("connection failed:".$conn-> connect_error);
}
return $conn;
}
}
要提取数据的类:
<?php
class SelectAMonthGet extends ConnectDB {
var $year;
var $month;
function __construct( ){
$this->year = $_POST['year'];
$this->month = $_POST['analyze_options_month'];
}
protected function SelectAMonthGetData(){
$year = $this->year;
$month = $this->month;
$sql = $this->connect()->prepare("SELECT * FROM wp_myexpenses WHERE YEAR(date) = ? AND MONTH(date) = ? order by date,id");
$sql->bind_param("ss",$year,$month);
$result = $sql ->execute();
$numRows = $result->num_rows;
if($numRows > 0) {
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
return $data;
}
}
}
?>
但是,即使DB中有数据,也不会显示任何结果。
使用代码时,如果没有如下所示的预备语句,我就能得到结果:
没有准备好的语句的代码(有效):
class SelectAMonthGet extends ConnectDB {
var $year;
var $month;
function __construct( ){
$this->year = $_POST['year'];
$this->month = $_POST['analyze_options_month'];
}
protected function SelectAMonthGetData(){
$year = $this->year;; $month = $this->month;
$sql = "SELECT * FROM wp_myexpenses WHERE YEAR(date) = '$year' AND MONTH(date) = '$month' order by date,id";
$result = $this->connect()->query($sql);
$numRows = $result->num_rows;
if($numRows > 0) {
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
return $data;
}
}
}
?>
我想使用准备好的语句来避免SQL注入。我无法理解我的代码出了什么问题?有人可以在这里引导我吗?
更新: 正如ADyson在评论中建议的那样,启用了错误报告。它给出以下错误:
Notice: Trying to get property of non-object in **** on line 34
第34行:
$numRows = $result->num_rows;
更新:
将代码更改为包含get_result(),如下所示
protected function SelectAMonthGetData(){
$year = $this->year;
$month = $this->month;
$sql = $this->connect()->prepare("SELECT * FROM wp_myexpenses WHERE YEAR(date) = ? AND MONTH(date) = ? order by date,id");
$sql->bind_param("ss",$year,$month);
$sql ->execute();
$result = $sql->get_result();
$numRows = $result->num_rows;
if($numRows > 0) {
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
return $data;
}
}
但是现在它给了错误提示:
Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::get_result() in ****:28
在检查某些解决方案时,错误是由于未启用mysqlnd驱动程序引起的。就我而言,这是真的。 请问有其他解决方案吗?
答案 0 :(得分:1)
下面是两个解决方案,第一个是通过数据库调用类型mysqli
,第二个是PDO
使用mysqli准备
bind_result
,不准备get_result()
function SelectAMonthGetData(){
$year = $this->year;
$month = $this->month;
///*****************
$mysqli=$this->connect();
/* Crée une requête préparée */
$stmt = $mysqli->prepare("SELECT date,col2 FROM ets WHERE (YEAR(date) = ? AND MONTH(date) = ?) ");
/* Lecture des marqueurs */
$stmt->bind_param("ss", $year,$month);
$stmt->execute();
/* bind result variables */
$stmt->bind_result($date, $col2); // here you can add your columns
/* fetch values */
while ($stmt->fetch()) {
$row=array();
$row['date']=$date;
$row['col2']=$col2;
$data[] = $row;
}
return $data;
}
使用
mysqli
和get_result()
function SelectAMonthGetData(){
$year = $this->year;
$month = $this->month;
///*****************
$mysqli=$this->connect();
/* Crée une requête préparée */
$stmt = $mysqli->prepare("SELECT * FROM ets WHERE YEAR(date) = ? AND MONTH(date) = ? ");
/* Lecture des marqueurs */
$stmt->bind_param("ss", $year,$month);
/* Exécution de la requête */
$result=$stmt->execute();
/* instead of bind_result: */
$result = $stmt->get_result();
$numRows = $result->num_rows;
if($numRows > 0) {
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
print_r($data);
return $data;
}
return null;
}
PDO
并使用:
绑定参数:
$sql = "SELECT * FROM wp_myexpenses WHERE YEAR(date) = :year AND MONTH(date) = :month order by date,id"
$stmt = $db_pdo->prepare($sql);
$stmt->execute(Array(':year' => $year, ':month' => $month));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);