我想在PHP中構建一個簡單的數據庫類,我正在使用MySQL。從MySQL獲取一行結果
現在我已經stucked了,我無法弄清楚如何顯示數據庫中的一行結果。我已經解決了如何從數據庫中獲得多行,但現在我試圖只顯示一個像用戶名,電子郵件,級別,reg_date。
我的代碼:
class Database {
public $mysql;
function __construct()
{
$this->mysql = new mysqli(host, user, password, db) or die('There was a problem connecting to the db');
}
function multiLine($sql)
{
$this->mysql->query("SET NAMES 'utf8'");
$this->mysql->query("SET CHARACTER SET 'utf8'");
if(($result = $this->mysql->query($sql)) != NULL) {
$x = array();
while($row = $result->fetch_array()) {
$x[] = $row;
}
return $x;
}
else {
echo $this->mysql->error;
}
}
function singleLine($sql)
{
$one = array();
if(($one = $this->mysql->query($sql)) != NULL)
{
$one = $result->fetch_array(); // Error here
}
return $one;
}
function __destruct() {
// close out the database connection;
$this->mysql->close();
}
}
我的功能多的作品,我使用它是這樣的:
$db = new Database();
$sql = 'SELECT * FROM users ORDER BY id DESC';
$response = $db->multiLine($sql);
<?php foreach($response as $r) : ?>
<p>ID: <?php echo $r['id']; ?></p>
<p>Username: <?php echo $r['username']; ?></p>
<?php endforeach; ?>
所以有人可以給我一個提示如何解決這個問題呢?或者也許有人有一個鏈接到一個偉大的tuturial在PHP中創建一個數據庫類。