Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

mysql - My PHP Scripts returns only one row from my table

I got a problem while I'm trying to retrieve data from db!

Here's my parsing code that I've included & will get the data from MySQL table:

$select_posts = "select * from posts order by RAND() LIMIT 0,6";
$run_posts = mysqli_query($con,$select_posts);
while($row=mysqli_fetch_array($run_posts)){
    $post_id = $row['post_id'];
    $post_title = $row['post_title'];
    $post_date = $row['post_date'];
    $post_author = $row['post_author'];
    $post_image = $row['post_image'];
    $post_keywords = $row['post_keywords'];
    $post_content = $row['post_content'];
    $post_summary = $row['post_summary'];
}

As you can see I add the LIMIT 0,6 which means "Starts from 1st row & show 6 items from table"...

But I don't know why only 1 result appears.

This is the page that I show results of my table:

<div id="box">
                <div class="BJadidBold">
                <?php 
                if (mysqli_num_rows($run_posts) > 0){ 
                    echo "
                    <h1>$post_title</h1>
                    <p>$post_summary</p></br>
                    <a style='float:right;font-size:30px;margin-top:-30px;' href='blog_post_content.php?post_id=$post_id'>readmore</a>
                    ";
                }else{
                    echo "<h1>No posts yet!</h1>";
                }
                ?>
                </div>
            </div>

I want to show at least 6 items on my page. How can I be able to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

change the first block to not overwrite:

$select_posts = "select * from posts order by RAND() LIMIT 0,6";
$run_posts = mysqli_query($con,$select_posts);
$post=array();
while($row=mysqli_fetch_array($run_posts)){
    $post[$row['post_id']]['id'] = $row['post_id'];
    $post[$row['post_id']]['title'] = $row['post_title'];
    $post[$row['post_id']]['date'] = $row['post_date'];
    $post[$row['post_id']]['author'] = $row['post_author'];
    $post[$row['post_id']]['image'] = $row['post_image'];
    $post[$row['post_id']]['keywords'] = $row['post_keywords'];
    $post[$row['post_id']]['content'] = $row['post_content'];
    $post[$row['post_id']]['summary'] = $row['post_summary'];
}

then loop the results for display:

foreach($post as $p){
 echo "
 <h1>$p['title']</h1>
 <p>$p['summary']</p></br>
 <a style='float:right;font-size:30px;margin-top:-30px;' href='blog_post_content.php?post_id=$p['id']'>readmore</a>";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...