AzizEFT Posted September 14, 2019 Share Posted September 14, 2019 Dear all, Help required in code: I have 2 tables (sites and photo_gallery) in my database. First I display my posts from database, then it goes to my viewpost page. Everything work file but in gallery portion my loop didn't work. IN TOP of the page I use this code to run all viewpost pages. if(isset($_GET['id'])) { require_once("_includes/LocalHost.php"); $id = mysqli_real_escape_string($db, $_GET['id']); $sql = "SELECT * FROM sites, photo_gallery WHERE gallery_con = photo_id AND id='$id' "; $result= mysqli_query($db, $sql) or die("Bad Query: $sql"); $row = mysqli_fetch_array($result); } In gallery portion <?php if($result->num_rows > 0){ while($row = $result->fetch_assoc()) { $title = $row["title_post"]; $imageURL = 'images/'.$row["photographs"]; $imageThumbURL = 'images/thumb_'.$row["photographs"]; ?> <a href="<?php echo $imageURL; ?>" class="image" data-fancybox="images"> <img src="<?php echo $imageThumbURL; ?>" alt="<?php echo $title; ?>" height="200px" /> </a> <?php } } ?> 1 to 15 images with every post and it will display only one 2nd image. Thank you for the help. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2019 Share Posted September 14, 2019 4 minutes ago, AzizEFT said: $result= mysqli_query($db, $sql) or die("Bad Query: $sql"); $row = mysqli_fetch_array($result); I don't know if it's your only problem but the fetch_array() call after the query is effectively throwing away the first row. You don't output anything until you read the second row in the while() statement at the start of the loop. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 16, 2019 Share Posted September 16, 2019 (edited) On 9/14/2019 at 9:03 AM, AzizEFT said: 1 to 15 images with every post and it will display only one 2nd image. Barand already explained why the first image is missing. Since it sounds like there should be more than 2 images, did you try displaying the value of $result->num_rows? That will tell you the number of rows actually returned. If you're not getting the expected value, the query may be the issue. Edited September 16, 2019 by cyberRobot fixed typo Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.