【问题标题】:mysqli connection file Doesnt recoqnize prepare stement [duplicate]mysqli连接文件不识别准备好的语句[重复]
【发布时间】:2020-03-30 11:09:28
【问题描述】:

我的 php mysqli 文件出现错误。

致命错误:未捕获的错误:调用成员函数 prepare() on 空入 C:\Users\driek\Desktop\xampp\htdocs\driekvandermeulen.nl_3.0\php\main\contact1.php:20 堆栈跟踪:#0 C:\Users\driek\Desktop\xampp\htdocs\driekvandermeulen.nl_3.0\php\main\contact1.php(33): writeDatabase('dsgd', 'Lorem@lorem.nl', 'gf', 'awesrdtfgh') #1 {main} 投入 C:\Users\driek\Desktop\xampp\htdocs\driekvandermeulen.nl_3.0\php\main\contact1.php 第 20 行

它没有识别第 20 行的准备语句,但我看不出它有什么问题。


    function dbConnect(){
        $servername = "localhost";                                          // Localhost stays untouched
        $username = "dbUser";                                               // Username of the MySQL database user
        $password = "R4b3Eg5Jt4Y9GKqB";                                         // Password of the MySQL database user
        $conn = new mysqli($servername, $username, $password);                  // Create connection
        if ($conn->connect_error) {                                                 // Check connection
            die("Connection failed: " . $conn->connect_error);
        }
        echo "Connected successfully";
        return;
    }

    function writeDatabase($p_sName,$p_sEmailAddress,$p_sSubject,$p_sMailContent){
       $conn = dbConnect();

    // -------------------- prepare and bind --------------------------------
        //$stmt = $conn->prepare("INSERT INTO `driekvandermeulen.tbl_emailcontacts` (`firstname`, `lastname`, `email`) VALUES (?, ?, ?, ?)");
        $stmt = $conn->prepare();
        $stmt->bind_param($p_sName,$p_sEmailAddress,$p_sSubject,$p_sMailContent);

                $stmt->execute(); // Actual save into the database
    }

【问题讨论】:

  • 试着在你的函数中说return $conn
  • 使用这个函数会导致“Too many connections”错误。根本不可能有这样的功能

标签: php mysql function mysqli


【解决方案1】:

dbConnect() 函数没有返回任何内容。因此,将dbConnect() 的结果分配给变量将产生null

$conn = dbConnect(); 这行将导致$conn = null;

您应该尝试从dbConnect() 函数返回连接对象:

function dbConnect(){
    $servername = "localhost";                                          // Localhost stays untouched
    $username = "dbUser";                                               // Username of the MySQL database user
    $password = "R4b3Eg5Jt4Y9GKqB";                                         // Password of the MySQL database user
    $conn = new mysqli($servername, $username, $password);                  // Create connection
    if ($conn->connect_error) {                                                 // Check connection
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    return $conn;  // <------------------ Check this
}

【讨论】:

  • 请随意改写我添加的评论,该评论使用您自己的风格指出函数中的修改
  • 很好地指出了正确的行。会提前做这件事。我就这样放着吧。
  • 使用这个函数会导致“Too many connections”错误。它将答案放在“解决一个问题,创建五个问题”类别下。
  • 好吧,我不是在“创造”新问题。它只是链条中的下一个。并不是说我的回答会产生新的问题。如果我的回答实际上是给他dbConnect() 函数,那么我会制造一个问题。我只是修复了他的语法问题。当然,我同意它的编写方式是最佳解决方案。但这超出了这个问题的范围。虽然它可以推荐给提出问题的人。
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 2012-12-01
  • 2011-02-02
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多