如何使用左连接从数据库中精确检索数据?

时间:2014-02-12 04:41:56

标签: php mysqli

大家好我有问题。

当我从数据库输入数据时,确切但当我从数据库中检索所有数据时是不一样的。 这是来自数据库的output,当我检索所有数据时,输出为this

我的问题是如何在数据库上输出相同内容......

我的代码..

$iqry = $mysqli->prepare("SELECT a.code, a.quantity, b.itemname, b.unitmeasure 
                FROM table_inventory a LEFT JOIN table_item b ON a.id=b.id");

$iqry->execute();

$iqry->bind_result($code,$quantity,$item,$unit);
$iqry->store_result();

while ($iqry->fetch()){
  $result[] = array("code"        => $code,
                    "quantity"    => $quantity,
                    "itemname"    => $item,
                    "unitmeasure" => $unit);
}

foreach ($result as $r){
  echo "<tr>";
  echo "<td>" .$r['code']. "</td>";
  echo "<td>" .$r['itemname']. "</td>";
  echo "<td>" .$r['quantity']. "</td>";
  echo "<td>" .$r['unitmeasure']. "</td>";
  echo "<td><a class='edit' href=''><img src='img/edit.png'>";
  echo "<td><a href=\"material-del.php\" class='confirm'><img src='img/delete.png'></a></td>";
  echo "</tr>";
}

1 个答案:

答案 0 :(得分:0)

试试这个

 $iqry = $mysqli->prepare("SELECT `table_inventory`.code, `table_inventory`.quantity, `table_item`.itemname, `table_item`.unitmeasure 
                    FROM `table_inventory ` LEFT JOIN `table_item` ON table_inventory.id=table_item.id order by `table_inventory`.id");

愿这对你有所帮助。

相关问题