php – mysql_fetch_array不适用于1行查询结果

前端之家收集整理的这篇文章主要介绍了php – mysql_fetch_array不适用于1行查询结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的查询工作正常.但是尝试从表中获取唯一值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行然后它不工作.

最佳答案
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'];
}
原文链接:https://www.f2er.com/mysql/434038.html

猜你在找的MySQL相关文章