mysqli 连接文件 Doesnt recoqnize prepare stement [duplicate]

mysqli connection file Doesnt recoqnize prepare stement [duplicate]

提问人:Driek van der Meulen 提问时间:12/5/2019 最后编辑:Funk Forty NinerDriek van der Meulen 更新时间:12/5/2019 访问量:36

问:

我的 php mysqli 文件出现错误。

致命错误:未捕获的错误:调用成员函数 prepare() on null 输入 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', '[email protected]', '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
    }
php mysql 函数 mysqli

评论

3赞 Masivuye Cokile 12/5/2019
尝试在函数中说return $conn
0赞 Your Common Sense 12/5/2019
使用此功能将导致“连接过多”错误。绝对不能有这样的功能

答:

3赞 Fjarlaegur 12/5/2019 #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
}

评论

1赞 Cid 12/5/2019
随意改写我添加的评论,该评论使用您自己的风格指出函数中的修改
0赞 Fjarlaegur 12/5/2019
指出更正后的行的好人。会提前这样做。我会这样离开。
0赞 Your Common Sense 12/5/2019
使用此功能将导致“连接过多”错误。它将答案放在“解决一个问题,创建五个问题”类别下。
1赞 Fjarlaegur 12/5/2019
好吧,我不是在“创造”新问题。它只是链条中的下一个。这并不是说我的答案会制造新的问题。如果我的答案实际上是给他这个功能,那么我就会制造一个问题。我只是解决了他的语法问题。当然,我同意它的编写方式是最佳解决方案。但这超出了这个问题的范围。虽然可以推荐给提出问题的人。dbConnect()