简体   繁体   中英

PHP Mysqli code not displaying query results

I want to output data from a query result. the query uses a print_r(json_encode($regions)) in another php page but it is not outputting anything. I have no errors in php, am I doing something wrong in mysqli code that it is not echoing anything?

//connecting to database
    <?php
    require_once('DbConnection.php');   
    //querying the database 
    $region_id = isset( $_GET['region_id'] )? $_GET['region_id']: false;
    $sql=mysqli_query($connection,"SELECT sales.region_id, sales.image_name,                 sales.price, sales.location, sales.Terms, sales.Contacts
    FROM sales INNER JOIN region ON sales.region_id=region.region_id  where region_id = $region_id") or die(mysqli_error($connection));
    $result = mysqli_query($connection,"SELECT sales.region_id, sales.image_name, sales.price, sales.location, sales.Terms, sales.Contacts FROM sales INNER JOIN region ON sales.region_id=region.region_id  where region_id = $region_id");             
    while ($row = mysql_fetch_assoc($sql)) {
             ?>
    <div class="col-md-4">
    <div class="thumbnail">
    <a href="<?php echo "http://" . $_SERVER['SERVER_NAME'] ?>/photo/imageuploads/<?php echo $row["image_name"]; ?>">
    <img src="<?php echo "http://" . $_SERVER['SERVER_NAME'] ?>/photo/imageuploads/<?php echo $row["image_name"]; ?>" alt="Lights" style="width:100%">
    <div class="caption">
    Image Name:<?php echo $row["image_name"]; ?>
    Price:<?php echo $row["price"]; ?>
    Location`enter code here`:<?php echo $row["location"]; ?>
    Terms:<?php echo $row["Terms"]; ?>
    Contacts:<?php echo $row["Contacts"]; ?>
    </div>
     </a>
    </div>
    </div>
    <?php
    }
    ?>

In your SQL, your where clause refers to region_id, which in this case is defined in two tables (sales and region), if you need both of these tables, then you need to qualify which table you want to use the region_id from

$sql=mysqli_query($connection,"SELECT sales.region_id, sales.image_name, 
                                sales.price, sales.location, sales.Terms, sales.Contacts
                   FROM sales     
                   INNER JOIN region ON sales.region_id=region.region_id  
                   where region.region_id = $region_id") or die(mysqli_error($connection));

but as you don't use any columns from region in your result, you could just drop the join...

$sql=mysqli_query($connection,"SELECT sales.region_id, sales.image_name, 
                                sales.price, sales.location, sales.Terms, sales.Contacts
                   FROM sales   
                   where region_id = $region_id") or die(mysqli_error($connection));

Also as Barmar says, remove the second execution of the query otherwise this may fail and stop the script as well.

Also where you check if $_GET['region_id'] , this should be more a case of if it isn't set, then don't do anything. Just setting it to false will cause more problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM