我正试图从数据库中获取数据,而while循环一直在迭代,找不到结果,这帮助我摆脱了这种情况


I m trying to fetch data from database and the while loop is keep on iterating unable to find the outcome help me to get rid of that

我创建了一个Connect.php文件用于连接到数据库。我还创建了一个php.ini文件。

<?php
session_start();
$_SESSION['user_id']= '1';
include 'db/connect.php';
include 'func/articles.php';
include 'func/like.php';
?>

还有一个从数据库和表中获取特定数据的文章文件:

article.php

<?php
    function get_articles()
                {
                include 'core/db/connect.php';
                    $articles = array();
                    $quere="SELECT article_id,article_title, article_likes FROM articles";
                    $query = mysqli_query($con,$quere);
                     while(($row = mysqli_fetch_assoc($query))!== false )
                        {
                        $articles[]=array(
                                            'article_id'    =>$row['article_id'],
                                            'article_title' =>$row['article_title'],
                                            'article_likes' =>$row['article_likes']
                                            );
                        }  
                        echo '<pre>'.print_r($articles, true).'</pre>';
                }   
?>

最后使用index.php文件同时调用它们:

    <?php
        $articles = get_articles();
        if(count($articles)==0)
            {
                echo "Sorry! There are no articles";
            }
            else
            {
                echo'ul';
                foreach($articles as $article)
                    {
                    echo '<li><p>',$article['article_title'],'</p><p><a href="#" >Like</a></p></li>';
                    }
                echo'</ul>';
            }
        ?>

它不起作用,它向我发送错误为:

允许的内存大小134217728字节已用尽(尝试分配64字节)

我真的不知道问题出在哪里。

只写while(($row = mysqli_fetch_assoc($query))) {...
如本例所示。

此外,请记住在get_articles()函数中写入一个return $articles;

将您的条件更改为

while(($row = mysqli_fetch_assoc($query)))

在脚本的顶部放置以下内容:

ini_set('memory_limit', '-1');

这将占用服务器上的无限内存使用量。

问题在于代码中while循环的条件不正确,而不是

while(($row = mysqli_fetch_assoc($query))!== false )

我们必须使用

while(($row = mysqli_fetch_assoc($query))!= false

在article.php

我在哪里使用

 echo '<pre>'.print_r($articles, true).'</pre>';

我必须使用

return $articles;