4

My query works fine. but wen trying get unique value from the table mysql fetch array not work.

this is my sql A-2815 is the item code of the vehicle. this field is unique. Expecting result is item_code A-2815 's vehicle details.

$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 ";

this is my php code.

<?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'];
      }
 ?>

this working ok when results are more then 1 row. but problem is when result is 1 row then it is not working.

5
  • 1
    Can you provide the result of single record which is not displaying Commented Feb 26, 2013 at 6:26
  • 1
    The 'mysql' extension is deprecated it seems as of 5.5.0? See the PHP.net Manual Page for more details. Commented Feb 26, 2013 at 6:38
  • You could also use heredoc to build your Query. It would remove the repetitive step to keep adding a new string to an existing variable. Commented Feb 26, 2013 at 6:45
  • Use PDO or mysqli_*. Consult this. Commented Feb 26, 2013 at 7:05
  • I have wacking my brain out to resolve this problem, but nothing helped me. Have anyone solution or any idea why is this happening? Commented Aug 4, 2013 at 0:33

1 Answer 1

3
while ($result = mysql_fetch_array($query, MYSQL_ASSOC)) {

    // assoc
    echo $result['item_code'];
}

MySQLi solution for this

$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'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is 'MYSQL_BOTH' not the default for mysql_fetch_array()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.