如何使用class& amp;获取php中的数据库功能oops概念

时间:2015-01-23 04:28:40

标签: php oop

我是php的新开发者。我在下面编写一个类和函数来连接数据库。但代码不工作如何创建类和函数oops概念帮助我。

class database {

#code
var $host = "localhost";
var $username ="root";
var $password ="";

var $dbase ="blue";
var $myconnect;

function connectTodata()
{
    $conn = mysql_connect($this->host, $this->username, $this->password);

    if(!$conn)
    {
        die("Cannot Connect to the datagase");
    }
    else
    {
        $his->myconnect = $conn;
        echo "Connect Established";
    }
    return $myconnect;

}
 function selectDatabase() // selecting the database.
{
    mysql_select_db($this->database);  //use php inbuild functions for select database

    if(mysql_error()) // if error occured display the error message
    {

        echo "Cannot find the database ".$this->database;

    }
     echo "Database selected..";       
}

function closeConnection() // close the connection
{
    mysql_close($this->myconn);

    echo "Connection closed";
   }
}

4 个答案:

答案 0 :(得分:0)

只是为了让它发挥作用:

class database 
{
    #code
    var $host = "localhost";
    var $username ="root";
    var $password ="";

    var $dbase ="blue";
    var $myconnect;

    function connectTodata()
    {
        $this->myconnect = mysql_connect($this->host, $this->username, $this->password);
        if(!$this->myconnect) die("Cannot Connect to the datagase");
        echo "Connect Established";
        return $this->myconnect;
    }

    function selectDatabase() // selecting the database.
    {
        mysql_select_db($this->database);  //use php inbuild functions for select database

        if(mysql_error()) // if error occured display the error message
        {
            echo "Cannot find the database ".$this->database;
        }
     echo "Database selected..";       
    }

    function closeConnection() // close the connection
    {
        mysql_close($this->myconnect);
        echo "Connection closed";
    }
}

答案 1 :(得分:0)

按照之前的海报所述修复错误,然后尝试启动:

$database_connection=new database();
$database_handle=$database_connection->connectTodata();
$database_connection->selectDatabase();

$database_connection->closeConnection();
unset($database_connection);
exit();

并且在代码块之间的空间中,您可以添加mysql查询并使用$ database_handle作为第二个参数。

答案 2 :(得分:0)

你可以使用构造函数,这样你就不需要每次都调用类函数:

 class database {
    var $host = "localhost";
    var $username ="root";
    var $password ="";

    var $dbase ="blue";
    var $myconnect;

    function __construct()  {
        $this->myconnect = mysql_connect($this->host, $this->username, $this->password);

       if( !$this->myconnect )  {
            $msg  = "could not connect to mysql database <Br/>";
            $msg .= mysql_error();
            die($msg);
        }

        $db_connect = mysql_select_db($this->dbase, $this->myconnect);

        if( !$db_connect )  {
            die(" can not select database \n".mysql_error());
        }
    }

    function __destruct() {
        mysql_close($this->myconnect);
    }
}

// call database 
$con = new database();

答案 3 :(得分:0)

你可以在定义类似

之类的帮助下完成
define("DB_SERVER_NAME","your host");
define("DB_USER_NAME","your user name");
define("DB_USER_PASSWORD","your password");
define("DB_DATABASE_NAME","your database");

现在连接文件:

class DBClass{
public $DB_LINK;
public $DB_DATABASE_NAME;
function DBClass($Main_DB = FALSE){
$this->DB_LINK = @mysqli_connect(DB_SERVER_NAME,DB_USER_NAME,DB_USER_PASSWORD,DB_DATABASE_NAME);
if(! $this->DB_LINK){
die("<h2>Could not connect to Database.</h2>");
}
}
function __destruct(){
mysqli_close($this->DB_LINK);
}   
}

这对我来说很完美