6

我的查询工作正常。但是试图从表 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 行时它不起作用。

4

1 回答 1

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'];
}
于 2013-02-26T06:28:35.443 回答