我的目标
我目前正在处理新闻源,并且正在研究 jQuery 使用的 ajax 知识。 我只是希望如果我发布一些东西,人们不必刷新他们的页面就可以看到新闻源。就像 facebook 和其他社交媒体网站一样。我想使用 jQuery AJAX 库从数据库中获取所有内容并将其回显
我在本主题中使用的语言:
PHP、SQL、jQuery AJAX、
我的问题
我的问题是......我已经搜索了一些好的代码,我认为有些代码可能有用,但每次我都会收到错误消息:
注意: undefined variable :C:\xampp\htdocs\__oop\logic\getNewsfeed.php 第 4 行中的 mysqli
fatal error :在第 4 行调用 C:\xampp\htdocs\__oop\logic\getNewsfeed.php 中非对象的成员函数 query()
我当然知道它说的是什么,但我真的想不通。
代码
好的,这是我的 AJAX(只要我将 $('.homepage_feed')
更改为其他内容,错误就会消失,但我当然需要它存储到输出数据。):
$(document).ready(function() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "logic/getNewsfeed.php",
dataType: "html", //expect html to be returned
success: function(response){
$(".homepage_feed").html(response);
//alert(response);
}
});
});
这是我的 PHP,我在其中选择所有内容并在 while 循环中包含一个包含我的 View (它回显 html)。错误是$user和$result查询根据SQL错误。
<div class="wrapper homepage_feed">
<?php
$user = mysqli_fetch_assoc($mysqli->query("SELECT * FROM users WHERE username='$ses_user'"));
$result = $mysqli->query("SELECT * FROM newsfeed ORDER BY posted_at DESC");
while ($post = $result->fetch_assoc()) {
$avatar = $post['avatar'] ? '<img src="avatars/' . $post['avatar'] . '" />' : '';
$imageValue = $post['image'] ? '<img src="uploads/' . $post['image'] . '" class="image_item" />' : '';
include('views/userFeed.php'); // shows newsfeed
}
?>
</div>
这是我的include('views/userFeed.php');
:
<?php
// newsfeed & profile content
// $imageValue checks if there is an image uploaded.
// if yes, then echo the image, else hide the complete <img> tag.
echo '
<article class="newsfeed_item transition">
<div class="post_avatar">' . $avatar . '</div>
<div class="user_details" title="'.$post['username'].'"><p>' . $post['username'] . '</p></div>
<div class="user_text"><p>' . $post['description'] . '</p></div>
' . $imageValue . '
<div class="amount_points_comments">
<p><span class="points_icon">' . $post['votes'] . '</span></p>
</div>
<a href="?action=updateVotes&vote=up&post_id='.$post['postID'].'&user_id='.$user['userID'].'">
<div class="vote_up_item"></div></a>
<div class="clear"></div>
</article>
';
?>
这是我的数据库设置:http://puu.sh/cTjxs/699b021dc5.png
如果有人知道如何消除这些错误,那就太好了。我真的很挣扎。也许我每次都看不到东西...... 非常感谢您的帮助。
谢谢。
最佳答案
变量 $mysqli
在您的代码中未定义。您使用它,但不为其设置任何值。所以错误告诉你了。查看 the docs 中 mysql 连接设置的示例 .
关于javascript - AJAX、PHP、SQL newsfeed 每次都会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26959968/