PHP 未定义函数
PHP undefined function
我目前正在处理 PHP 和 MySQL 中的消息显示。
我总是在第 22 行收到此错误:
Fatal error: Call to undefined function query() in ...
但是我在这个特定的行中定义了 $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);
?>
有什么想法吗?
预先感谢您的帮助
错别字:
$result = $db-query($query);
^---- mathematical subtraction
(例如 "contents of $db minus result of undefined function query()")
你想要
$result = $db->query($query);
^^----object member operator
我目前正在处理 PHP 和 MySQL 中的消息显示。
我总是在第 22 行收到此错误:
Fatal error: Call to undefined function query() in ...
但是我在这个特定的行中定义了 $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);
?>
有什么想法吗? 预先感谢您的帮助
错别字:
$result = $db-query($query);
^---- mathematical subtraction
(例如 "contents of $db minus result of undefined function query()")
你想要
$result = $db->query($query);
^^----object member operator