loop within loop in PHP MySQL

526

I have created two tables in MySql one where blog content saved and another where blog ID and different pics path saved. I have saved more than one pics in one postID. Now I am trying to fetch the pics and blog post at same time but its not working. code below...

blogposts.php

<?php
// Check connection
if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($conn,"SELECT * , upload_data.FILE_NAME from blog_posts LEFT JOIN upload_data ON blog_posts.postID=upload_data.postID ; ");
while($row = mysqli_fetch_array($result)) 
{?>

 <div data-height="382">
          <ul><?php 
          if (!empty($FILE_NAME)) {
          $result = mysqli_query($conn,"SELECT * from upload_data where postID=postID ; ");
        while($row = mysqli_fetch_array($result)) 

          {?>
              <li data-transition="3dcurtain-vertical" data-slotamount="12">
              <img data-retina src="articles/user_data/<?php echo( htmlspecialchars( $row['FILE_NAME'] ) ); ?>">
            </li> <?php }} ?>
           </ul>
        </div>
 <div >
    Title : <a href="#" ><?php echo( htmlspecialchars( $row['postTitle'] ) ); ?></a>, <a href="#" ><?php echo( htmlspecialchars( $row['postCat'] ) ); ?></a>
    <span ></span>
    Tag : <a href="#" >Nllam</a>
    <span ></span>
    <a href="#" ><i ></i>12 Comments</a>
  </div>
<?php } ?>

The Problem is I am unable to fetch the pics. :(

847

Answer

Solution:

use this query

$result = mysqli_query($conn,"SELECT  blog_posts.* , upload_data.FILE_NAME from blog_posts,upload_data WHERE blog_posts.postID=upload_data.postID");

and then print your results with print_r() function and check its get results or not.

728

Answer

Solution:

SELECT blog_posts.*, group_concat(upload_data.FILE_NAME) FROM `blog_posts` 
LEFT JOIN upload_data on blog_posts.postID = upload_data.postID 
GROUP BY blog_posts.postID

Try like this you will get number of post rows and filename concadinated with commas if you have images. then Apply logic to display results. Any help you need. Don't hesitate to ask me.

247

Answer

Solution:

hope this work;

$post_id = 1337;
$conn = new PDO(......); //get connection
$conn->query('SET group_concat_max_len = 4096;');
$query = "SELECT bp.* , 
      (SELECT GROUP_CONCAT(ud.FILE_NAME SEPARATOR ',')  
       from upload_data ud where ud.postID=bp.postID) as files
     FROM blog_posts bp 
     WHERE bp.postID=:post_id ;";
$result = $conn->prepare($query);
$result->bindParam(":post_id", $post_id);
$result->execute();

//don't really need while for one row but oh well
while($row = $result->fetch()){

  $files=$row['files'];
  $files = empty($files) ? [] : explode(',', $files);
  ?>

  <div data-height="382">
      <ul>
      <?php foreach ($files as $file){?>
        <li data-transition="3dcurtain-vertical" data-slotamount="12">
          <img data-retina src="articles/user_data/<?php echo( htmlspecialchars( $file ) ); ?>">
        </li> 
      <?php } ?>
      </ul>
  </div>
  <div >
    Title : <a href="#" ><?php echo( htmlspecialchars( $row['postTitle'] ) ); ?></a>, <a href="#" ><?php echo( htmlspecialchars( $row['postCat'] ) ); ?></a>
    <span ></span>
    Tag : <a href="#" >Nllam</a>
    <span ></span>
    <a href="#" ><i ></i>12 Comments</a>
  </div>
<?php } ?>

People are also looking for solutions to the problem: while loop not returning results PHP/MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.