mysql — PHP неопределенная функция

В настоящее время я работаю над отображением сообщений в PHP и MySQL.
Я всегда получаю эту ошибку в строке 22:

Неустранимая ошибка: вызов неопределенной функции query () в …

Но я определил $ query в этой конкретной строке ….
Мой код выглядит следующим образом:

    <?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbmitarbeiter";

// Create connection
$db = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}

$useremp = $_SESSION['username'];

$query = "SELECT *
FROM `tbabonnenten` s
WHERE dest = " . $db->real_escape_string($useremp);

$result = $db-query($query);

if (!$result) {
echo 'SQL error ' . mysql_error();
exit;
}

$user = $result->fetch_assoc();

/*
* Run this query, and fetch the entire userrow, and put into $user
* An alternative could be to just store the entire user array into
* $_SESSION, as you wont have to query the database twice.
*/

$tofetch = array();

for ( $i=1; $i<=3; $i++ )
{
//Let's check msg1, msg2, msg3
if ( $user['abo' . $i] )
{
$tofetch[] = "name = 'Forum" . $i . "'";
}
}

/*
* If tbsubscribers has a '1' in msg1, and a '1' in msg2 then the array
* will look like this:
* array(
* 0 => "name = 'msg1'"* 1 => "name = 'msg2'"* )
*/

//Throw an exception if all 'msg1', 'msg2', and 'msg3' are all 0 (Otherwise the query will fail)
if ( empty($tofetch) ) {
echo "no subscriptions to show";
//Don't continue here, otherwise the query will fail.
exit;
}

/*
* Now it's a simple query. $tofetch will be imploded, to form a nice
* where statement. Using the example above, it will look like:
* name = 'msg1' OR name = 'msg2'
*/

$query = "SELECT *
FROM `tbmitteilung`
WHERE " . implode(' OR ', $tofetch);

$result = $db->query($query);

if (!$result) {
echo 'SQL error ' . mysql_error();
exit;
}

$messages = $result->fetch_all(MYSQLI_ASSOC);

print_r($messages);



?>

Есть идеи?
заранее спасибо за помощь

-2

Решение

Опечатки:

$result = $db-query($query);
^---- mathematical subtraction

(например, «содержимое $ db минус результат неопределенной функции query ()»)

Ты хочешь

$result = $db->query($query);
^^----object member operator
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector