-1

我正在尝试使用为所有 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 资源

我尝试在连接函数中调用变量,所以函数 disconnect(){ $close = mysqli_close($connect);}

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

4

3 回答 3

1

问题是变量范围。该disconnect()函数没有引用$connect.

我不建议这样做。虽然我也不推荐包装类,但它比此类功能更好。而是考虑通过依赖注入共享的类、单例或全局对象

于 2013-07-08T21:39:19.537 回答
0

将您的 disconnect() 函数更改为:

function disconnect()
{
mysqli_close();
}

或者

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

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

于 2013-07-09T02:48:42.427 回答
-1

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

配置文件

 <?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';

}   
}

}
?>

连接类.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();
?>

其背后的理论是数据库详细信息保存在 config.php 中,该文件通过 bootstrap.php 拉入,然后加载附加测试的类。除了更改为 mysqli 和删除测试之外,任何人都可以看到我是否可以在安全性或在大型站点周围轻松使用方面进行改进。感谢先进

于 2013-07-10T10:40:23.527 回答