Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_array not working for 1 row query result

Tags:

php

mysql

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.

like image 291
Nytram Avatar asked Feb 26 '13 06:02

Nytram


People also ask

What is the difference between mysql_fetch_array () and Ysql_fetch_object ()?

The mysqli_fetch_object() function returns objects from the database, whereas mysqli_fetch_array() function delivers an array of results. This will allow field names to be used to access the data.

What is mysql_fetch_array () function?

mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate. Syntax.

What is the use of mysql_fetch_row () in mysql?

mysql_fetch_row() retrieves the next row of a result set: When used after mysql_store_result() , mysql_fetch_row() returns NULL if there are no more rows to retrieve.

What does Mysqli_fetch_array return?

The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both. Note: Fieldnames returned from this function are case-sensitive.


1 Answers

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'];
}
like image 100
Madan Sapkota Avatar answered Sep 28 '22 07:09

Madan Sapkota