我对此感到非常沮丧。我正在尝试使用 mysqli 创建一个包含列值的数组。现在,当我回显返回的内容时,它什么也没打印。我已经尝试了很多事情,我无法弄清楚这一点。
<?php
class Database {
public $dbHost = '';
public $dbUser = '';
public $dbPass = '';
public $dbName = '';
public $db;
public function __construct(){
$this->dbConnect();
}
public function dbConnect(){
$this->db = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
/* check connection */
if (mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}else{
echo 'connection made';
}
}
public function q($sql){
$query = $sql;
self::preparedStatement($query);
}
public function preparedStatement($query){
$i= 0; //index
$result = array();
if ($stmt = $this->db->prepare($query)){
/* execute statement */
if($stmt->execute()) {
$stmt->bind_result($name);
while($stmt->fetch()) {
$result[0] = $name;
}
} else
echo "error";
/* close statement */
$stmt->close();
} else {
echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
}
return $result;
}
public function __destruct(){}
}
### Test code
$db = new Database();
$res = $db->q("SELECT name FROM test");
echo $res[0];
?>