$sql = "SELECT * FROM product_suplier LEFT JOIN furnizor ON furnizor.id_furnizor=product_suplier.id_furnizor WHERE product_suplier.id_product=$_GET[id])";
$result = $conn->query($sql);
while($row_furnizor = $result->fetch_assoc()) {
echo $row_furnizor['name'].' - adresa: '.$row_furnizor['adresa']; }
PHPMyadmin结果是:
nr_id ... | ... id_product ... | ... id_suplier ... | ... id_suplier ... | ... name ... | ... adress
2 ......... | ... 2 .................. | ... 1 ......... .......... | .... 1 ............... | ...凯文。| ...阿姆斯特丹
3 ......... | ... 2 .................. | ... 2 ............ ...... | ...... 2 ............... | ......关心...... | ......伦敦
错误:致命错误:在非对象上调用成员函数fetch_assoc()
答案 0 :(得分:1)
主要问题可能是缺少GET变量名称周围的引号 - 尽管没有逻辑测试来决定是否处理记录集。您可能还发现在连接表时更容易分配表别名,因为它使sql更容易阅读IMO。
<?php
/* only proceed if there is an `id` ~ the $_GET variable required quotes */
$id=isset( $_GET['id'] ) && !empty( $_GET['id'] ) ? strip_tags( trim( $_GET['id'] ) ): false;
if( $id ){
/* prepare the sql ( not prepared statement )*/
$sql = "select * from
`product_suplier` p
left outer join `furnizor` f on f.`id_furnizor`=p.`id_furnizor`
where p.`id_product`='{$id}'";
/* Assign a result to the query */
$result = $conn->query( $sql );
/* logical test on the query result - only proceed if it succeeded */
if( $result ){
/* Personal preference: using object notation rather than array */
while( $rs = $result->fetch_object() ) {
echo $rs->name.' - adresa: '.$rs->adresa;
}
}
}
?>
答案 1 :(得分:0)
将此行更新为:
$sql = "SELECT * FROM product_suplier LEFT JOIN furnizor ON
furnizor.id_furnizor=product_suplier.id_furnizor WHERE
product_suplier.id_product=".mysqli_escape_string($_GET['id']);
以无效方式调用$_GET
数组值,同时删除)
,当您从用户获取数据时应使用mysql_escape_string
函数以防止SQL注入。