免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
php – OOP數(shù)據(jù)庫連接/斷開類

我剛剛開始學(xué)習(xí)面向?qū)ο缶幊痰母拍?并將一個用于連接數(shù)據(jù)庫,選擇數(shù)據(jù)庫和關(guān)閉數(shù)據(jù)庫連接的類組合在一起.到目前為止,除了關(guān)閉與數(shù)據(jù)庫的連接外,一切似乎都沒問題.

    class Database {    private $host, $username, $password;    public function __construct($ihost, $iusername, $ipassword){        $this->host = $ihost;        $this->username = $iusername;        $this->password = $ipassword;    }    public function connectdb(){        mysql_connect($this->host, $this->username, $this->password)            OR die("There was a problem connecting to the database.");        echo 'successfully connected to database<br />';    }    public function select($database){        mysql_select_db($database)            OR die("There was a problem selecting the database.");        echo 'successfully selected database<br />';    }    public function disconnectdb(){        mysql_close($this->connectdb())            OR die("There was a problem disconnecting from the database.");    }}$database = new database('localhost', 'root', 'usbw');$database->connectdb();$database->select('msm');$database->disconnectdb();

當(dāng)我嘗試斷開與數(shù)據(jù)庫的連接時,我收到以下錯誤消息:

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in F:\Programs\webserver\root\oop\oop.php on line 53

我猜這不像在mysql_close函數(shù)的括號中放置connectdb方法那么簡單,但找不到正確的方法.

謝謝

解決方法:

我會在你的類中添加一個連接/鏈接變量,并使用析構(gòu)函數(shù).
這也將使您不必忘記關(guān)閉連接,因為它會自動完成.
您需要傳遞給mysql_close()的是$this->鏈接.

class Database {    private $link;    private $host, $username, $password, $database;    public function __construct($host, $username, $password, $database){        $this->host        = $host;        $this->username    = $username;        $this->password    = $password;        $this->database    = $database;        $this->link = mysql_connect($this->host, $this->username, $this->password)            OR die("There was a problem connecting to the database.");        mysql_select_db($this->database, $this->link)            OR die("There was a problem selecting the database.");        return true;    }    public function query($query) {        $result = mysql_query($query);        if (!$result) die('Invalid query: ' . mysql_error());        return $result;    }    public function __destruct() {        mysql_close($this->link)            OR die("There was a problem disconnecting from the database.");    }}

用法示例:

<?php    $db = new Database("localhost", "username", "password", "testDatabase");    $result = $db->query("SELECT * FROM students");    while ($row = mysql_fetch_assoc($result)) {        echo "First Name: " . $row['firstname'] ."<br />";        echo "Last Name: "  . $row['lastname']  ."<br />";        echo "Address: "    . $row['address']   ."<br />";        echo "Age: "        . $row['age']       ."<br />";        echo "<hr />";    }?>

編輯:
所以人們實際上可以使用這個類,我添加了缺少的屬性/方法.
下一步是擴(kuò)展查詢方法,包括注入保護(hù)和任何其他幫助函數(shù).
我做了以下更改:

>添加了缺少的私有屬性
>添加了__construct($host,$username,$password,$database)
>將connectdb()和select()合并到__construct()中,以節(jié)省額外的兩行代碼.
>添加了查詢($query)
>示例用法

如果我寫了一個錯字或錯誤,請留下建設(shè)性的評論,以便我可以為其他人解決.

編輯23/06/2018

正如所指出的那樣,mysql已經(jīng)過時了,因為這個問題仍然定期訪問,我想我會發(fā)布一個更新的解決方案.

class Database {    private $mysqli;    private $host, $username, $password, $database;    /**     * Creates the mysql connection.     * Kills the script on connection or database errors.     *      * @param string $host     * @param string $username     * @param string $password     * @param string $database     * @return boolean     */    public function __construct($host, $username, $password, $database){        $this->host        = $host;        $this->username    = $username;        $this->password    = $password;        $this->database    = $database;        $this->mysqli = new mysqli($this->host, $this->username, $this->password)            OR die("There was a problem connecting to the database.");        /* check connection */        if (mysqli_connect_errno()) {            printf("Connect failed: %s\n", mysqli_connect_error());            exit();        }        $this->mysqli->select_db($this->database);        if (mysqli_connect_errno()) {            printf("Connect failed: %s\n", mysqli_connect_error());            exit();        }        return true;    }    /**     * Prints the currently selected database.     */    public function print_database_name()    {        /* return name of current default database */        if ($result = $this->mysqli->query("SELECT DATABASE()")) {            $row = $result->fetch_row();            printf("Selected database is %s.\n", $row[0]);            $result->close();        }    }    /**     * On error returns an array with the error code.     * On success returns an array with multiple mysql data.     *      * @param string $query     * @return array     */    public function query($query) {        /* array returned, includes a success boolean */        $return = array();        if(!$result = $this->mysqli->query($query))        {            $return['success'] = false;            $return['error'] = $this->mysqli->error;            return $return;        }        $return['success'] = true;        $return['affected_rows'] = $this->mysqli->affected_rows;        $return['insert_id'] = $this->mysqli->insert_id;        if(0 == $this->mysqli->insert_id)        {            $return['count'] = $result->num_rows;            $return['rows'] = array();            /* fetch associative array */            while ($row = $result->fetch_assoc()) {                $return['rows'][] = $row;            }            /* free result set */            $result->close();        }        return $return;    }    /**     * Automatically closes the mysql connection     * at the end of the program.     */    public function __destruct() {        $this->mysqli->close()            OR die("There was a problem disconnecting from the database.");    }}

用法示例:

<?php    $db = new Database("localhost", "username", "password", "testDatabase");    $result = $db->query("SELECT * FROM students");    if(true == $result['success'])    {        echo "Number of rows: " . $result['count'] ."<br />";        foreach($result['rows'] as $row)        {            echo "First Name: " . $row['firstname'] ."<br />";            echo "Last Name: "  . $row['lastname']  ."<br />";            echo "Address: "    . $row['address']   ."<br />";            echo "Age: "        . $row['age']       ."<br />";            echo "<hr />";        }    }    if(false == $result['success'])    {        echo "An error has occurred: " . $result['error'] ."<br />";    }?>
來源:https://www.icode9.com/content-2-323701.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
PHP中如何連接數(shù)據(jù)庫
mysqli->multi_query()執(zhí)行多條語句
數(shù)據(jù)庫的備份和還原
PHP+MySQL會員系統(tǒng)功能
php 后臺數(shù)據(jù)庫與前臺請求結(jié)合
MySQL Select from 查詢數(shù)據(jù)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服