lately I've been getting an error undefined variable mysqli and I was wondering - is it because I'm accessing two different tables in the same database?
$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;
}
The $mysqli
object is not getting recognized inside get_new_session_number()
.
Use the global
keyword:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.