试图使用PHP的OOP方法我得到No rows
从MySQLi的获取数据,同时我相信我有比赛排在数据库
我有存储在一个文件中db
一个名为db.inc.php
类,它是像
<?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;
}
}
?>
我有一个在SetData
称为SetData.inc.php
扩展类,它像
<?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();
}
}
?>
终于在头版我有
<?PHP
$page = 'game';
$region = 'ASIA';
include '../inc/SetData.inc.php';
$cls = new SetData();
$cls->SetBox($page, $region);
我不知道dbconnect()
是什么,你需要在这里打电话给你connect()
方法:
//$this->conn = new dbconnect(); // NO!
$this->conn = $this->connect();
此外,你不应该在这里呼吁connect()
,你已经在$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` = ?");
然后,你要什么有$title
和description
办?也许归还?
$stmt->bind_result($titlerow, $descriptionrow);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
return array('title' => $titlerow, 'description' => $descriptionrow);
然后调用SetBox()
并显示:
$result = $cls->SetBox($page, $region);
echo $result['title'];
或者设置属性:
$stmt->bind_result($titlerow, $descriptionrow);
$stmt->fetch();
$this->title = $titlerow;
$this->description = $descriptionrow;
$stmt->free_result();
$stmt->close();
然后调用SetBox()
并显示:
$cls->SetBox($page, $region);
echo $cls->title;