0

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?

1
  • 2
    in your loop you overwrite each variable. you will end up with the last db row in each of those variables Commented Jan 24, 2016 at 20:44

1 Answer 1

1

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>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot but I think I need to change $post['title'] to $p['title'] in order to do that for displaying results!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.