mysqli类使用预处理语句不返回任何数据
16lz
2021-01-22
Here is the class, I wrote. There are no errors when I execute the findbyAdNetID method. However, the method is not fetching any data. The same code works if I put it outside a class and remove instance variables $this everywhere. Any help would be appreciated. Thanks.
我写道,这是课程。执行findbyAdNetID方法时没有错误。但是,该方法不提取任何数据。如果我将它放在一个类之外并且在任何地方删除实例变量$ this,则相同的代码可以工作任何帮助,将不胜感激。谢谢。
<?php
class adscraper_mysqli
{
private $id;
private $result;
private $rows;
private $_mysqli;
private $statement;
private $query ="SELECT * FROM scrapelist_master WHERE ad_network_id=?";
public function __construct($host = NULL, $username = NULL, $password = NULL, $db = NULL)
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->connect();
}
/**
* A method to connect to the database
*
*/
public function connect()
{
$this->_mysqli = new mysqli ($this->host, $this->username, $this->password, $this->db)
or die('There was a problem connecting to the database');
}
/**
* A method to create prepared statements
*
*/
public function prepare()
{
$this->statement = $this->_mysqli->prepare($this->query);
}
/* Method to query using prepared statement */
public function findbyAdNetID($id){
if (!$this->statement) {
$this->prepare();
}
$this->statement->bind_param("i", $this->id);
$this->statement->execute();
$this->result = $this->statement->get_result();
$this->rows = $this->result->fetch_all(MYSQLI_ASSOC);
return $this->rows;
}
} // END class
?>
`
1 个解决方案
#1
All the code is written in the right way. The only problem, why you've got nothing is, that in findbyAdNetID($id) method is row:
所有代码都以正确的方式编写。唯一的问题,为什么你什么都没有,在findbyAdNetID($ id)方法是行:
$this->statement->bind_param("i", $this->id);
But you didn't set $this->id. So you have to use only $id instead of, like
但你没有设置$ this-> id。所以你必须只使用$ id代替
$this->statement->bind_param("i", $id);
or set it at the beginning
或者在开头设置它
$this->id = id;
更多相关文章
- PHP用空格分割文本为数组的方法
- 几个有用的php字符串过滤,转换函数代码
- 高效的敏感词过滤方法(PHP)
- php static静态变量及方法详解
- laravel 4路由::控制器()方法返回NotFoundHttpException。
- PHP上传文件 Error 6解决方法
- 基于PHP的UUID/GUID/uniqid替换方法及其可靠性论述
- 有一种防弹的方法可以检测php字符串中的base64编码吗?
- PHP实现接口方法时出现致命错误