我的查询工作正常。但是尝试从表中获取唯一值mysql fetch数组不起作用。
这是我的sql A-2815是车辆的物品代码。这个领域是独特的。期待的结果是item_code A-2815的车辆细节。$sql = "SELECT vehicle.id, item_code, make, model, vehicle_type, color, showroom_id, ";
$sql .= "adding_user_Id, approved, image_1 ";
$sql .= "FROM images, vehicle ";
$sql .= "WHERE vehicle.id=images.item_id ";
$sql .= "AND (item_code LIKE '%A-2815%' ";
$sql .= "OR make LIKE '%A-2815%' ";
$sql .= "OR model LIKE '%A-2815%' ";
$sql .= "OR vehicle_type LIKE '%A-2815%' ";
$sql .= "OR color LIKE '%A-2815%' ";
$sql .= "OR showroom_id LIKE '%A-2815%') ";
$sql .= "AND activate=1 ";
$sql .= "AND type_of_image=1 ";
这是我的PHP代码。
<?php
$query = mysql_query($sql);
while($result = mysql_fetch_array($query)){
echo $result['item_code'];
echo $result['make'];
echo $result['model'];
echo $result['vehicle_type'];
echo $result['color'];
echo $result['showroom_id'];
}
?>
当结果超过1行时,这工作正常。但问题是当结果是1行然后它不工作。
答案 0 :(得分:3)
while ($result = mysql_fetch_array($query, MYSQL_ASSOC)) {
// assoc
echo $result['item_code'];
}
MySQLi解决方案
$connect = mysqli_connect('localhost', 'root', 'pwd', 'dbname');
$sql = "select * from sometable";
$query = mysqli_query($connect, $sql);
while ($result = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
// assoc
echo $result['item_code'];
}