dongye7231 2018-09-22 08:04
浏览 105
已采纳

PHP Mysqli代码不显示查询结果

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
    }
    ?>
  • 写回答

1条回答 默认 最新

  • doushi4795 2018-09-22 08:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度