繁体   English   中英

mysqli类使用准备好的语句不返回任何数据

[英]mysqli class using prepared statement not returning any data

这是我写的课。 执行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
 ?>

`

所有代码都以正确的方式编写。 唯一的问题,为什么您什么都不做,是在findbyAdNetID($ id)方法中是row:

    $this->statement->bind_param("i", $this->id);

但是您没有设置$ this-> id。 因此,您只需要使用$ id即可,例如

    $this->statement->bind_param("i", $id);

或将其设置在开头

    $this->id = id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM