简体   繁体   中英

MySQLi Prepare Statement and OOP PHP Query Returns 0 Row

Trying to get data from MySQLi using PHP OOP apporoach I am getting No rows while I am sure I have match row in the database

I have a class called db stored in a file as db.inc.php and it is like

<?PHP
class db {
    private $DBSERVER;
    private $DBUSERNAME;
    private $DBPASSWORD;
    private $DBNAME;

    protected function connect(){
      $this->DBSERVER   = "localhost"; 
      $this->DBUSERNAME = "root"; 
      $this->DBPASSWORD = ""; 
      $this->DBNAME     = "maator"; 

      $conn = new mysqli($this->DBSERVER, $this->DBUSERNAME, $this->DBPASSWORD, $this->DBNAME);
      if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
      }     
      return $conn;
    }
}
?>

I have an extended class called SetData in SetData.inc.php which like

<?PHP
include_once('db.inc.php'); 
class SetData extends db {
    private $page;
    private $region;
    private $conn;

    function __construct() {
       $this->conn = new db();
    }

   public function SetBox($vpage, $vregion){
        $this->page     = $vpage;
        $this->region = $vregion;
        $stmt = $this->conn->connect()->prepare("SELECT `title`,`description` FROM html WHERE `page` = ? AND `region` = ?");
        $stmt->bind_param("ss", $this->page, $this->region);    
        $stmt->execute();
        $stmt->store_result();
       if($stmt->num_rows === 0) exit('No rows');
        $stmt->bind_result($titlerow,$descriptionrow);
        $stmt->fetch();
            $title = $titlerow;
            $description = $descriptionrow;
        $stmt->free_result();
        $stmt->close();
    }
}
?>

and finally in front page I have

<?PHP
$page = 'game';
$region = 'ASIA';
include '../inc/SetData.inc.php';
$cls = new SetData();
$cls->SetBox($page, $region);

I don't know what dbconnect() is, you need to call your connect() method here:

//$this->conn = new dbconnect(); // NO!

$this->conn = $this->connect();

Also, you shouldn't call connect() here, you already have a connection in $conn :

//$stmt = $this->conn->connect()->prepare("SELECT `title`,`description` FROM html WHERE `page` = ? AND `region` = ?"); // NO!

$stmt = $this->conn->prepare("SELECT `title`,`description` FROM html WHERE `page` = ? AND `region` = ?");

Then, what do you want to do with $title and description ? Maybe return them?

    $stmt->bind_result($titlerow, $descriptionrow);
    $stmt->fetch();
    $stmt->free_result();
    $stmt->close();

    return array('title' => $titlerow, 'description' => $descriptionrow);

Then call SetBox() and display:

$result = $cls->SetBox($page, $region);
echo $result['title'];

Or set properties:

    $stmt->bind_result($titlerow, $descriptionrow);
    $stmt->fetch();

    $this->title = $titlerow;
    $this->description = $descriptionrow;

    $stmt->free_result();
    $stmt->close();

Then call SetBox() and display:

$cls->SetBox($page, $region);
echo $cls->title;

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