简体   繁体   中英

mysqli class using prepared statement not returning any data

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.

<?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
 ?>

`

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:

    $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->statement->bind_param("i", $id);

or set it at the beginning

    $this->id = id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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