当我在http://localhost/phpmyadmin/上运行查询时,此查询出了什么问题 它会显示我想要的结果,但在PHP页面上运行时会产生不同的结果。
但是当我在PHP页面上获取结果时,它也会给出空记录
如何在没有空SubCategoryID和SubCategory Name
的情况下获取记录这是PHP代码
<table id="subCat1Table" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Sub Category ID</th>
<th>Sub Category Name</th>
<th>Root Category ID</th>
<th>Root Category Name</th>
</tr>
</thead>
<!-- <tfoot>
<tr>
<th>Sub Category ID</th>
<th>Sub Category Name</th>
<th>Root Category ID</th>
<th>Root Category Name</th>
</tr>
</tfoot> -->
<tbody>
<?php
$query1 = mysqli_query($conn, 'SELECT * FROM sub_category1 LEFT JOIN main_category ON sub_category1.main_category_id = main_category.category_id ');
while ($row = mysqli_fetch_array($query1, MYSQLI_ASSOC))
{
// Result Assigning to Array
$rows[] = $row;
}
// traversing Array
echo("<pre>");
print_r($rows);
echo("</pre>");
foreach($rows as $row):
// Getting Values
$subCategory1ID = stripslashes($row['sub_category1_id']);
$subCategory1Name = stripslashes($row['sub_category1_name']);
$rootCategoryID = stripslashes($row['category_id']);
$rootCategoryName = stripslashes($row['category_name']);
?>
<tr>
<td><?php echo "$subCategory1ID"; ?> </td>
<td><?php echo "$subCategory1Name"; ?> </td>
<td><?php echo "$rootCategoryID"; ?> </td>
<td><?php echo "$rootCategoryName"; ?> </td>
</tr>
<?php
endforeach; //End ForEach loop
?>
</tbody>
</table>
答案 0 :(得分:2)
LEFT JOIN关键字返回左表(table1)中的所有记录,以及右表(table2)中的匹配记录。如果没有匹配,则结果从右侧为NULL。
您应该使用内部联接来仅提取匹配记录
答案 1 :(得分:0)
此处出现的问题是由于在while循环中为数组赋值
<?php
$query1 = mysqli_query($conn, 'SELECT * FROM sub_category1 LEFT JOIN main_category ON sub_category1.main_category_id = main_category.category_id ');
while ($row = mysqli_fetch_array($query1, MYSQLI_ASSOC))
{
// Result Assigning to Array
$rows[] = $row;
}
// traversing Array
echo("<pre>");
print_r($rows);
echo("</pre>");
foreach($rows as $row):
// Getting Values
$subCategory1ID = stripslashes($row['sub_category1_id']);
$subCategory1Name = stripslashes($row['sub_category1_name']);
$rootCategoryID = stripslashes($row['category_id']);
$rootCategoryName = stripslashes($row['category_name']);
?>
<tr>
<td><?php echo "$subCategory1ID"; ?> </td>
<td><?php echo "$subCategory1Name"; ?> </td>
<td><?php echo "$rootCategoryID"; ?> </td>
<td><?php echo "$rootCategoryName"; ?> </td>
</tr>
<?php
endforeach; //End ForEach loop
?>
现在没有分配到数组
<?php
$query = mysqli_query($conn, "SELECT * FROM sub_category1 INNER JOIN main_category ON sub_category1.main_category_id = main_category.category_id");
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)):
// Getting Values
$subCategory1ID = stripslashes($row['sub_category1_id']);
$subCategory1Name = stripslashes($row['sub_category1_name']);
$mainCatID= stripslashes($row['main_category_id']);
$rootCategoryID = stripslashes($row['category_id']);
$rootCategoryName = stripslashes($row['category_name']);
?>
<tr>
<td><?php echo "$subCategory1ID"; ?> </td>
<td><?php echo "$subCategory1Name"; ?> </td>
<td><?php echo "$mainCatID"; ?> </td>
<td><?php echo "$rootCategoryID"; ?> </td>
<td><?php echo "$rootCategoryName"; ?> </td>
</tr>
<?php
endwhile; //End ForEach loop
?>