mysqli连接和断开连接作为功能错误

时间:2013-07-08 21:36:17

标签: php function mysqli

我正在尝试使用为所有mysqli命令设置的php函数创建自己的工作模板

现在这是我的 config.php

<?php

/** Configuration Variables **/

define ('DEVELOPMENT_ENVIRONMENT',true);

if (DEVELOPMENT_ENVIRONMENT == true) {
define('DB_NAME', 'rdb');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
} else {
 define('DB_NAME', 'db47');
define('DB_USER', 'dbo');
define('DB_PASSWORD', 'S');
define('DB_HOST', 'd.db.1and1.com');

这是一个我正在调用 sql_functions.php

的页面
<?php
//create db connection function
function connect()
{
$connect = mysqli_connect(DB_HOST , DB_USER , DB_PASSWORD, DB_NAME) 
or die ("Failed to connect to MySQL: " . mysqli_connect_error()); 
}

//create db disconect function
function disconnect()
{
$close = mysqli_close();
}

connect();
disconnect();

?>

现在我已经测试了连接功能,但断开连接功能不断出现以下错误 警告:mysql_close():第12行的R:\ UniServer \ www \ application \ functions \ sql_functions.php中没有提供MySQL-Link资源

我试过在connect函数中调用变量     function disconnect(){     $ close = mysqli_close($ connect);}

但这仍然会带来错误。 。 。请帮助提前感谢,我仍在学习,所以宁愿我的错误解释,以便我可以纠正而不是只为我写的答案:)

3 个答案:

答案 0 :(得分:1)

问题是variable scopedisconnect()函数没有引用$connect

我不建议这样做。虽然我也不推荐使用包装类,但它比这些函数更好。请考虑通过class, singleton, or global object共享dependency injection

答案 1 :(得分:0)

将disconnect()函数更改为:

function disconnect()
{
mysqli_close();
}

function disconnect()
{
mysqli_close($connect);
}

如果将函数定义为变量,则不会执行该函数。可能会有所帮助:D

答案 2 :(得分:-1)

这是我目前的解决方案,但最终将在mysqli

的config.php

 <?php
/** Configuration Variables **/

define ('DEVELOPMENT_ENVIRONMENT',true);

class Dbconfig {
protected $host;
protected $username;
protected $password;
protected $database;

function Dbconfig(){
if (DEVELOPMENT_ENVIRONMENT == true) {
$this -> host =  'localhost';
$this -> username = 'root';
$this -> password = 'root';
$this -> database= 'db';

} else {
$this -> host =  '.db.1and1.com';
$this -> username = 'dbo';
$this -> password = 'SW';
$this -> database = 'db4';

}   
}

}
?>

connect.class.php

<?php

class createConnection  extends Dbconfig //create a class for make connection
{

function connectToDatabase() // create a function for connect database
{

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

    if(!$conn)// testing the connection
    {
        die ("Cannot connect to the database");
    }

    else
    {

        $this->myconn = $conn;

        echo "Connection established";

    }

    return $this->myconn;

}

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";
}

//now test the connection

} 
$connection = new createConnection(); //i created a new object

$connection->connectToDatabase(); // connected to the database

echo "<br />"; // putting a html break

$connection->selectDatabase();// closed connection

echo "<br />";

$connection->closeConnection();
?>

它背后的理论是db详细信息保存在config.php中,它通过bootstrap.php引入,然后类加载测试附加。除了改为mysqli并删除测试,任何人都可以看到我可以改善这一点,因为在安全性或易于在大型网站周围使用。谢谢高级