【问题标题】:Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in... Fatal error: Call to a member function query() on null in [duplicate]警告:mysqli_real_escape_string() 需要 2 个参数,1 个在...中给出 致命错误:在 [重复]
【发布时间】:2015-11-18 09:20:30
【问题描述】:

我的数据库连接有问题,我真的不明白我应该做什么。谁能帮帮我?

警告:mysqli_real_escape_string() 需要 2 个参数,1 在线/customers/f/4/7/zebratv.se/httpd.www/classes/db.php 中给出 47 致命错误:在 null 上调用成员函数 query() /customers/f/4/7/zebratv.se/httpd.www/classes/db.php 在第 61 行

<?php
class db
{

    public function __construct()
    {
        global $CONFIG;
        $mysqli = $CONFIG->get("database");
        $this->dbconn($mysqli["host"], $mysqli["username"], $mysqli["password"], $mysqli["database"]);

    }
    private function dbconn($mysqli_host, $mysqli_user, $mysqli_pass, $mysqli_db)
    {


        if (!($conn = new mysqli($mysqli_host, $mysqli_user, $mysqli_pass)))
        {
          switch (mysqli_errno($conn))
          {
            case 1040:
            case 2002:
                if ($_SERVER[REQUEST_METHOD] == "GET")
                    die("<html><head><meta http-equiv=refresh content=\"5 $_SERVER[REQUEST_URI]\"></head><body><table border=0 width=100% height=100%><tr><td><h3 align=center>The server load is very high at the moment. Retrying, please wait...</h3></td></tr></table></body></html>");
                else
                    die("Too many users. Please press the Refresh button in your browser to retry.");
            default:
                die("[" . mysqli_errno($conn) . "] dbconn: mysqli_connect: " . mysqli_error($conn));
          }
        }
        mysqli_select_db($conn, $mysqli_db)
            or die('dbconn:  mysqli_select_db: ' + mysqli_error($conn));

        usercontrol::userlogin();
    }
    public function sql_query($query)
    {    
        @++$_SESSION['totalqueries'];
        return $conn->query($query);
    }

    public function sanitize($var)
    {
        return($this->sqlesc((htmlspecialchars($var))));
    }
    function sqlesc($x) {
         return "'".mysqli_real_escape_string($x)."'";
    }
    public function sqlerr($file = '', $line = '')
    {
      print("<table border=0 bgcolor=blue align=left cellspacing=0 cellpadding=10 style='background: blue'>" .
        "<tr><td class=embedded><font color=white><h1>SQL Error</h1>\n" .
      "<b>" . mysqli_error() . ($file != '' && $line != '' ? "<p>in $file, line $line</p>" : "") . "</b></font></td></tr></table>");
      die;
    }
    public function get_row_count($table, $suffix = "")
    {
        if ($suffix)
            $suffix = " $suffix";
        $query = "SELECT COUNT(*) FROM ".$table."".$suffix;
        $r = $conn->query($query) or die(mysqli_error());
        $a = $conn->fetch_row($r) or die(mysqli_error());
        return $a[0];
    }

}
$DB = new db;
?>

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    mysqli_real_escape_string 函数需要数据库链接作为第一个参数。所以改变

    mysqli_real_escape_string($x)
    

    mysqli_real_escape_string($conn, $x)
    

    【讨论】:

      【解决方案2】:

      问题出在这里:

      function sqlesc($x) {
           return "'".mysqli_real_escape_string($x)."'";
      }
      

      错误很简单——当你应该提供两个参数时,你只提供了一个参数。 Here's the documentation of the function.

      mysqli_real_excape_string() 期望第一个参数是当前 mysqli 连接,第二个参数是字符串:

      mysqli_real_excape_string($conn,$x);
      

      您显然需要先将该变量传递给函数。

      【讨论】:

      • 现在我明白了。你能帮我么?在第 8 行和第 8 行的 /customers/f/4/7/zebratv.se/httpd.www/classes/db.php 中调用 null 上的成员函数 get() 是 $mysqli = $CONFIG->get("数据库");
      • $CONFIG 未定义。你在哪里定义的?它不在代码中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 2011-12-06
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      相关资源
      最近更新 更多