使用mysql在非对象上出错prepare()

时间:2014-02-28 21:59:17

标签: php mysqli

我收到PHP致命错误:在第30行的/Config/functions.php中调用非对象的成员函数prepare()。

的functions.php

function login($email, $password, $mysqli) {


if ($stmt = $mysqli->prepare("SELECT * 
    FROM users
   WHERE email = ?
    LIMIT 1")) {
    $stmt->bind_param('s', $email); 
    $stmt->execute();   
    $stmt->store_result();

$ mysqli从另一个已包含的文件中的常量中获取数据

if (login($email, $password, $mysqli)     == true) {
    // Login success 
    header('Location: ../protected_page.php');
} else {
    // Login failed 
    header('Location: ../login.php?error=1');
}

1 个答案:

答案 0 :(得分:-1)

你的意思是这样吗?

// Contents of constants.php
define("HOST", "localhost");
define("USER", "root");
define("PASS", "pass");
define("DATABASE", "test");
// End of constants.php

include('constants.php');
$mysqli = new mysqli(HOST, USER, PASS, DATABASE);

function login($email, $password) 
{
    if ($stmt = $mysqli->prepare("SELECT * FROM users WHERE email = ? LIMIT 1")) 
    {
        $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();
    }
}