调用一个方法用mysqli连接到数据库

您好我想在我的PHP脚本中从MySQL迁移到MySQLi,我有这个系统类,其功能是 使用如下所示的简单方法连接到我需要的数据库:</ p>

  Sys :: database_connect(); 
</ code> </ pre> \ n

函数的实际代码是:</ p>

  function database_connect(){
require'conf.php'; //带有数据库变量的配置文件( sql _)
mysql_connect($ sql_serv,$ sql_user,$ sql_pw)或死掉('ERRO !!!nãeligoua base de dados');
mysql_select_db($ sql_bd);
}
</ code> < / pre>

在调用函数后我可以毫无问题地查询数据库。</ p>

但如果我把它放在sys类中,我就不能对mysqli做同样的事情: </ p>

  function database_connect(){
require'conf.php';

$ mysqli = new mysqli($ sql_serv,$ sql_user,$ sql_pw,$ sql_bd) ;
if(mysqli_connect_errno()){
printf(“LigaçãoàBasede dados falhou:%s \ n“,mysqli_connect_error());
exit();
}
}
</ code> </ pre>

当我调用
Sys :: database_connect(); < / p>

它连接到数据库,但我无法查询已经习惯了我想要的一个简单的方法,就像我用普通的MySQL做的那样,如果有人能解释我在做什么 错误或为什么我不能这样做... </ p>

提前谢谢你;
Fernando Andrade。</ p>
</ div>

展开原文

原文

Hi i am trying to move from MySQL to MySQLi in my PHP script, i had this system class with the function that connects to the database that i call whenever i need, with a simple method like this:

Sys::database_connect();

the actual code of the function is:

function database_connect(){
    require 'conf.php';//configuration file with database variables (sql_)
    mysql_connect($sql_serv, $sql_user, $sql_pw) OR die('ERRO!!! não ligou a base de dados');
    mysql_select_db($sql_bd);
}

after calling the function i can query the database without problem.

But I cant do the same with mysqli if I put this in the sys class:

function database_connect(){
    require 'conf.php';

    $mysqli = new mysqli($sql_serv, $sql_user, $sql_pw, $sql_bd);
        if (mysqli_connect_errno()) {
        printf("Ligação à Base de dados falhou: %s
", mysqli_connect_error());
        exit();
    }
}

When I call Sys::database_connect();

it connects to the database but i can't query has i used to what i would like would be a simple method as I have done with normal MySQL, if somebody can explain what am I doing wrong or why exactly I cannot do it like that...

Thank you in advance; Fernando Andrade.

2个回答



您以后的查询必须使用mysqli连接标识符,您在连接数据库时创建。 因此,将其保存到系统类的属性中,稍后再使用它。</ p>

  function database_connect(){
require'conf.php';

$ mysqli = new mysqli($ sql_serv,$ sql_user,$ sql_pw,$ sql_bd);
if(mysqli_connect_errno()){
printf(“LigaçãoàBasede dados falhou:%s
”,mysqli_connect_error()); \ n exit();
}

Sys :: $ dbConn = $ mysqli;
}
</ code> </ pre>

然后在</ p> \ n

 函数查询($ sql){
Sys :: $ dbConn-&gt; query($ sql);
//错误处理等
}
</ code> < / pre>
</ div>

展开原文

原文

Your later queries have to use the mysqli connection identifier, you create when connecting to the database. So save this to a property of your system class and use it later on.

function database_connect(){
    require 'conf.php';

    $mysqli = new mysqli($sql_serv, $sql_user, $sql_pw, $sql_bd);
    if (mysqli_connect_errno()) {
        printf("Ligação à Base de dados falhou: %s
", mysqli_connect_error());
        exit();
    }

    Sys::$dbConn = $mysqli;
}

and then later on

function query( $sql ) {
    Sys::$dbConn->query( $sql );
    // error handling etc.
}

dtvdz911959
dtvdz911959 错过了示例代码中的$。
7 年多之前 回复
douxi4414
douxi4414 这是完整的错误:“解析错误:语法错误,意外'='在(...)在线(...)”
7 年多之前 回复
dtziv24262
dtziv24262 我宣布它是公开考虑的:)但是错误怎么样我无法测试这个因为页面不会加载...“...错误意外'='on ...”该行是'Sys: :dbConn = $ mysqli;' 写的是@Sirko
7 年多之前 回复
douwei2966
douwei2966 取决于您分配属性dbConn的可见性。
7 年多之前 回复
dongsui4658
dongsui4658 我可以在其他课程中使用它吗? 我尝试使用这个,但脚本在我声明Sys :: dbConn的行上给了我一个意外错误'='的错误
7 年多之前 回复



我为mysqli数据库类编写了一个扩展</ p>

 类数据库扩展mysqli {

function __construct(){
parent :: __ construct('host','user','password','database');
if(mysqli_connect_error()){
die('Connect error('。 mysqli_connect_errno()。')'。mysqli_connect_error());
}
parent :: query(“SET NAMES'utf8'”);
}

函数查询($ query){
$ result = parent :: query($ query);
if(!$ result){
echo“&lt; strong&gt; MySQL错误&lt; / strong&gt;:”。$ this-&gt;错误。“&lt; br /&gt; QUERY :“。$ query;
die();
}
if(!is_object($ result)){
$ result = new mysqli_result($ this);
}
返回$ result;
}
}
</ code> </ pre>

因此连接到数据库是$ db = new database()
执行查询是$ db-&gt; query(YOUR QUERY); < / p>
</ div>

展开原文

原文

I wrote an extend for the mysqli database class

class database extends mysqli {
    function __construct() {
        parent::__construct('host', 'user', 'password', 'database');
        if(mysqli_connect_error()) {
            die('Connect error ('.mysqli_connect_errno().')'.mysqli_connect_error());
        }
        parent::query("SET NAMES 'utf8'");
}

function query($query) {
        $result = parent::query($query);
        if(!$result) {
            echo "<strong>MySQL error</strong>: ".$this->error."<br />QUERY: ".$query;
            die();
        }
        if(!is_object($result)) {
            $result = new mysqli_result($this);
        }
        return $result;
    }
}

So connecting to database is $db = new database() Executing a query is $db->query(YOUR QUERY);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问