-2
我目前正在處理PHP和MySQL中的消息顯示。
我總是在第22行此錯誤:
PHP未定義函數
Fatal error: Call to undefined function query() in ...
但我在這個特定行定義的$查詢....
我的代碼如下:
<?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);
?>
任何想法? 感謝您的幫助提前
錯字:'$ db-query($ query);'應該是'$ db-> query($ query);' – Barmar
閱讀錯誤信息。它沒有說**變量**'$ query'有問題,它說問題是用未定義的**函數** query()' – Barmar
這不會幫助你'mysql_error( )'=> http://php.net/manual/en/mysqli.error.php –