gpt4 book ai didi

php - 警告 : Invalid argument supplied for foreach() in C:\wamp\www\social\includes\class-query. php 第 145 行

转载 作者:行者123 更新时间:2023-11-30 00:23:10 24 4
gpt4 key购买 nike

我的 foreach 语句出现无效参数错误。我正在尝试创建一个页面,从我的数据库中检索消息,并将其显示在我的收件箱中。这是发生错误的代码。

   public function do_inbox($user_id) {
$message_objects = $this->get_message_objects($user_id);

Line 145 --------> foreach ( $message_objects as $message ) {?>
<div class="status_item">
<?php $user = $this->load_user_object($message->message_sender_id); ?>
<h3>From: <a href="/social/profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $message->message_subject; ?></p>
<p><?php echo $message->message_content; ?></p>
</div>

这是我的完整代码:

<?php
//class-query.php
require_once('class-db.php');

if ( !class_exists('QUERY') ) {
class QUERY {
public function load_user_object($user_id) {
global $db;

$table = 's_users';

$query = "
SELECT * FROM $table
WHERE ID = $user_id
";

$obj = $db->select($query);

if ( !$obj ) {
return "No user found";
}

return $obj[0];
}

public function load_all_user_objects() {
global $db;

$table = 's_users';

$query = "
SELECT * FROM $table
";

$obj = $db->select($query);

if ( !$obj ) {
return "No user found";
}

return $obj;
}

public function get_friends($user_id) {
global $db;

$table = 's_friends';

$query = "
SELECT ID, friend_id FROM $table
WHERE user_id = '$user_id'
";

$friends = $db->select($query);

foreach ( $friends as $friend ) {
$friend_ids[] = $friend->friend_id;
}

return $friend_ids;
}

public function get_status_objects($user_id) {
global $db;

$table = 's_status';

$friend_ids = $this->get_friends($user_id);

if ( !empty ( $friend_ids ) ) {
array_push($friend_ids, $user_id);
} else {
$friend_ids = array($user_id);
}

$accepted_ids = implode(', ', $friend_ids);

$query = "
SELECT * FROM $table
WHERE user_id IN ($accepted_ids)
ORDER BY status_time DESC
";

$status_objects = $db->select($query);

return $status_objects;
}

public function get_message_objects($user_id) {
global $db;

$table = 's_messages';

$query = "
SELECT * FROM $table
WHERE message_recipient_id = '$user_id'
";

$messages = $db->select($query);

return $messages;
}

public function do_user_directory() {
$users = $this->load_all_user_objects();

foreach ( $users as $user ) { ?>
<div class="directory_item">
<h3><a href="/social/profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $user->user_email; ?></p>
</div>
<?php
}
}

public function do_friends_list($friends_array) {
foreach ( $friends_array as $friend_id ) {
$users[] = $this->load_user_object($friend_id);
}

foreach ( $users as $user ) { ?>
<div class="directory_item">
<h3><a href="/social/profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $user->user_email; ?></p>
</div>
<?php
}
}

public function do_news_feed($user_id) {
$status_objects = $this->get_status_objects($user_id);

foreach ( $status_objects as $status ) {?>
<div class="status_item">
<?php $user = $this->load_user_object($status->user_ID); ?>
<h3><a href="/social/profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $status->status_content; ?></p>
</div>
<?php
}
}

public function do_inbox($user_id) {
$message_objects = $this->get_message_objects($user_id);

foreach ( $message_objects as $message ) {?>
<div class="status_item">
<?php $user = $this->load_user_object($message->message_sender_id); ?>
<h3>From: <a href="/social/profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $message->message_subject; ?></p>
<p><?php echo $message->message_content; ?></p>
</div>
<?php
}
}
}
}

$query = new QUERY;
?>

这是我的插入 php 页面,它运行我的大部分查询:

<?php
//class-insert php page
require_once('class-db.php');


if ( !class_exists('INSERT') ) {
class INSERT {
public function update_user($user_id, $postdata) {
global $db;

$table = 's_users';

$query = "
UPDATE $table
SET user_email='$postdata[user_email]', user_pass='$postdata[user_pass]', user_nicename='$postdata[user_nicename]'
WHERE ID=$user_id
";

return $db->update($query);
}


public function add_friend($user_id, $friend_id) {
global $db;

$table = 's_friends';

$query = "
INSERT INTO $table (user_id, friend_id)
VALUES ('$user_id', '$friend_id')
";

return $db->insert($query);
}

public function remove_friend($user_id, $friend_id) {
global $db;

$table = 's_friends';

$query = "
DELETE FROM $table
WHERE user_id = '$user_id'
AND friend_id = '$friend_id'
";

return $db->insert($query);

}

public function add_status($user_id, $_my_post) {
global $db;

$table = 's_status';

$query = "
INSERT INTO $table (user_id, status_time, status_content)
VALUES ($user_id, '$_my_post[status_time]', '$_my_post[status_content]')
";

return $db->insert($query);

}

public function send_message($_my_post) {
global $db;

$table = 's_messages';

$query = "
INSERT INTO $table (message_time, message_sender_id, message_recipient_id, message_subject, message_content)
VALUES ('$_my_post[message_time]', '$_my_post[message_sender_id]', '$_my_post[message_recipient_id]', '$_my_post[message_subject]', '$_my_post[message_content]')
";

return $db->insert($query);
}
}
}


$insert = new INSERT;
?>

我还收到一条通知: undefined variable :导致第 38 行 C:\wamp\www\social\includes\class-db.php这是我的类数据库页面上。这是代码:

<?php
//class-db.php
if ( !class_exists ('DB') ) {
class DB {
public function __construct() {
$mysqli = new mysqli('localhost', 'root', 'test123', 'social');

if ($mysqli->connect_errno) {
printf("Connect failed %s\n", $mysqli->connect_error);
exit();
}

$this->connection = $mysqli;
}

public function insert($query) {
$result = $this->connection->query($query);

return $result;
}

public function update($query) {
$result = $this->connection->query($query);

return $result;
}

public function select($query) {
$result = $this->connection->query($query);

if ( !$result ) {
return false;
}

while ( $obj = $result->fetch_object() ) {
$results[] = $obj;
}

Line 38 -----------------> return $results;
}
}
}

$db = new DB;
?>

如有任何帮助,我们将不胜感激。我正在尝试在 youtube 上复制本教程:https://www.youtube.com/watch?v=Wgbmgf84ZOo

最佳答案

您必须像这样更改 class-db.php 中的选择函数:

public function select($query) {                            
$result = $this->connection->query($query);

if ( !$result->fetch_object()) {
$obj = $result->fetch_object();
$results = array();
do {
array_push($results, array($obj));
while($obj = $result->fetch_object());
}
return $results;
}

关于php - 警告 : Invalid argument supplied for foreach() in C:\wamp\www\social\includes\class-query. php 第 145 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com