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.
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.
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.
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.
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.
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'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With