0

最近、undefined variable mysqli というエラーが発生し、疑問に思っていました。同じデータベース内の 2 つの異なるテーブルにアクセスしているからでしょうか?

$mysqli = new mysqli('localhost', 'myuser', 'qwerty', 'mydb');

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
    //initialise vars
    $username = $_POST['username'];

    $checkRecord = $mysqli->query("SELECT information, blahblah FROM table1 WHERE username='$username' AND field1='$field1' AND field2=$field2");

    if(!$checkRecord->num_rows) 
    {
        echo "nothing retreived";
    }   
    else
    {
        $catch = $checkRecord->fetch_object();

        $newsessionnumber = get_new_session_number($username, $ip, $userAgent);

        $senddata = array(
            'newsessionnumber' => $newsessionnumber,
            'info' => $catch->information, 
            'blahblah' => $catch->blahblah, 
            'username' => $username 
        );

        echo json_encode($senddata); 
    }   
}

function get_new_session_number($username, $ip, $userAgent)
{   
    $mysqli->query("INSERT INTO sessionnumberdispatch VALUES (NULL, '$username', NOW(), '$ip', '$userAgent')");

    $newsessionnumber = $mysqli->insert_id;
    return $newsessionnumber;       
}
4

1 に答える 1

2

$mysqliオブジェクトは内部で認識されていませんget_new_session_number()

globalキーワードを使用します:

function get_new_session_number($username, $ip, $userAgent)
{   
    global $mysqli;
    $mysqli->query("INSERT INTO sessionnumberdispatch VALUES (NULL, '$username', NOW(), '$ip', '$userAgent')");

    $newsessionnumber = $mysqli->insert_id;
    return $newsessionnumber;       
}
于 2012-10-11T18:34:22.203 に答える