我想从查询结果中输出数据。该查询在另一个php页面中使用print_r(json_encode($regions))
,但未输出任何内容。我在php中没有错误,我在mysqli代码中做错什么了吗,它没有回显任何东西?
//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
}
?>
答案 0 :(得分:0)
在您的SQL中,where子句引用region_id,在这种情况下,它是在两个表(sales和region)中定义的,如果您需要这两个表,则需要限定要使用哪个表来使用region_id来自
$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));
但是由于您不使用结果中来自区域的任何列,因此可以删除联接...
$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));
也如Barmar所说,删除查询的第二次执行,否则可能会失败并停止脚本。
还要检查$_GET['region_id']
是否在哪里,这应该是更多的情况,如果未设置,则什么也不做。只是将其设置为false会导致更多问题。