douwen3836 2014-05-13 21:36
浏览 36
已采纳

函数内的mysqli_connect包含失败,为什么?

I've seen a few people with this same issue, yet none of the answers solved my problem. This is what I have in three files:

index.php:

 $dbconnect = connectDB("mydb","connect");
 mysqli_query($dbconnect,"INSERT INTO `mytable` (field1,field2) values ('value 1','value 2')");
 connectDB("","kill",$dbconnect);

functions.php

 function connectDB($db_name,$connectorkill,$link) { include('connectDB.inc.php'); }

connectDB.inc.php

 $host="localhost";
 $user="myusername";
 $pass="mypassword";
 if($connectorkill == "connect") {  
 if($dblink = mysqli_connect($host, $user, $pass, $db_name)) {
 echo "dblink created.";
  return $dblink;
 } else { echo "Error connecting to database."; }  
 } elseif($connectorkill == "kill") { mysqli_close($link); }

and what I get is:

 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/username/public_html/members/index.php on line 2

which looks like the connectDB function is not doing its job, specifically the return of the $dblink variable in connectDB.inc.php. I went ahead and moved the code from connectDB.inc.php to inside the functions.php and it solved the problem, but I don't want it set up that way. Is there a way to solve this error?

  • 写回答

1条回答 默认 最新

  • dongou4052 2014-05-13 21:46
    关注

    Two solutions:

    Solution 1: Refractor your code, something along the lines of:

    config.php:

    $config = array(
        "databases" => array(
            "my_database" => array(
                "host"  =>  "localhost",
                "user"  =>  "myusername",
                "pass"  =>  "mypassword"
            )
        )
    );
    

    functions.php:

    require("config.php");
    
    function connectDB($dbname)
    {
        global $config;
    
        if (!isset($config["databases"][$dbname]))
            return; // error, database info not in $config
    
        $dbc = $config["databases"][$dbname];
    
        $dbcon = mysqli_connect($dbc["host"], $dbc["user"], $dbc["pass"], $dbname);
        if (!$dbcon)
            return; // unable to connect error goes here
    
        return $dbcon;
    }
    

    Note that this is just the bare minimum of a connectDB function. There really should be more error checking in there (for example, on each of the $dbc keys. It also needs proper error reporting and such, this is just a general hint in the right direction.

    Solution 2: Add return in front of your include...

    function connectDB($db_name,$connectorkill,$link) { return include('connectDB.inc.php'); }
    

    Regardless of the solution: You really need some error checking code in your index.php because no matter what you do: Your connectDB function isn't always going to return a valid database connection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频