AJAX,PHP,SQL newsfeed每次都会出错

时间:2014-11-16 17:35:36

标签: javascript php jquery mysql ajax

我的目标

我目前正在处理新闻源,我正在研究jQuery使用的ajax知识。 我只是想要,如果我发布一些东西,人们不必刷新他们的页面来看新闻源。就像Facebook和其他社交媒体网站一样。我想从数据库中获取所有内容并使用jQuery AJAX库

进行回应

本主题中使用的语言:

PHP,SQL,jQuery AJAX,

我的问题

我的问题是......我已经搜索了一些好的代码,我觉得有些代码可能有效,但每次说出来都会出错:

Notice: Undefined variable: mysqli in C:\xampp\htdocs\__oop\logic\getNewsfeed.php on line 4

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\__oop\logic\getNewsfeed.php on line 4

我知道它说的是什么,但我真的无法理解。

CODE

好的,这是我的 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-loop中包含了我的视图(它是echo的html)。根据SQL错误,错误是$ user和$ result查询。

<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&amp;vote=up&amp;post_id='.$post['postID'].'&amp;user_id='.$user['userID'].'">
            <div class="vote_up_item"></div></a>
            <div class="clear"></div>
        </article>
    ';
?>

这是我的数据库设置:http://puu.sh/cTjxs/699b021dc5.png

如果有人知道如何摆脱这些错误,那就太好了。我真的很挣扎。也许我不是每次都看到的东西...... 非常感谢帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

您的代码中未定义变量$mysqli。你使用它,但没有设置任何值。所以错误告诉了你。查看the docs中的mysql连接设置的示例