函数内的mysqli准备语句

mysqli prepared statement inside a function

所以我正在尝试 运行 函数内的准备语句,但似乎一直难以传递 mysqli 连接语句。到目前为止,我肯定已经超过了 100 次组合尝试...

在函数中使用它之前,我将连接用于另一个准备好的语句。由于难以将 in include() 传递给函数

,我什至在语句中声明了我的连接变量
include('phpsqlinfo_dbinfo.php');
$mysqli = new mysqli($host, $dbusername, $dbpassword, $database);

$stmt = $mysqli->prepare("SELECT id, email, username, password, gender, homelat")

//blah blah blah run a statement here. I omitted this statement for brevity purposes.

$stmt->close();
$mysqli->close();


function storeTokenForUser($username,$token) {
    echo "running2";

    echo $token;
    echo $username;
    $host="";
    $dbusername="XXXX";
    $dbpassword="XXXX";
    $database="XXXX";

    global $mysqli;

    $stmt=$mysqli->prepare("INSERT INTO cookielogin (username, token) VALUES (?,?)"); 
    $stmt->bind_param('ss', $username, $token1);
    $stmt->execute();
    if (mysqli_connect_errno()) {
            echo("error".htmlspecialchars($mysqli->error).htmlspecialchars($stmt1->error));
    }
    echo"running3";
}


function cookie($username,$mysqli) {
$token = md5(uniqid(mt_rand(), true));
storeTokenForUser($username, $token);
$cookie = $username . ':' . $token;
$mac = hash_hmac('sha256', $cookie, 9842);
$cookie .= ':' . $mac;
setcookie('rememberme', $cookie);
        echo"running1";
}

我在努力的过程中遇到了不同的错误,但现在,这给了我:

Warning: mysqli::prepare(): Couldn't fetch mysqli in /var/www/loginphp.php on line 63

Fatal error: Call to a member function bind_param() on a non-object in /var/www/loginphp.php on line 64

衷心感谢您的帮助。非常感谢。

删除:

$stmt->close();
$mysqli->close();

修复错误并:

$stmt->bind_param('ss', $username, $token1);

$token1 未定义。