如何在php中发送对象/数据库连接到函数


how to send object/database connection to function in php

我打开一个SQL连接,并试图将其发送到另一个函数,它不工作。可能是和sintax有关的东西,我在网上到处找都找不到。

my code:

$mainDataBase = mysqli_connect(/...../);
if ($mainDataBase->connect_errno) {
    printf("Connect failed: %s'n", $mainDataBase->connect_error);
    die;
}
/**
 * 1 -> logIn && getInformation
 * 
 * */
switch ($whichOperation){
case 1:
    $gettingPlayerNumber = checkIfPlayerExist($mainDataBase, $playerName, $playerPass);
    if($gettingPlayerNumber == 0){
        echo "F";
        die;
    }
    ... keep going ...

功能::

function checkIfPlayerExist($dataBase ,$playerName ,$pass ){
    $result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = " . $playerPass .")");
    $row = $result->fetch_assoc();
    if($row != null)
        return $row['playerNumber'];
    return 0;
}
其说:

在非对象上调用成员函数fetch_assoc()。

感谢您的宝贵时间。

  • 之后的一些答案我检查和$结果是空的,但我检查了查询之前和现在,它的工作原理!pass是整型,所以在查询中不需要任何"。

已成功连接到数据库-我也检查了这个

您的查询中似乎有一个错误,靠近密码,缺少引号:

改变,

 $result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = " . $playerPass .")");

 $result = $dataBase->query("select playerNumber from PlayerInformation where (playerAccount = '" . $playerName ."') && (playerPass = '" . $playerPass ."')");