0

So I have a SQL database with a number of objects in that contain name price and images, I would like to know how I can select them using php and output the result into json

<?php
$db = mysqli_connect ('localhost', 'root', '', 'car_rental') or die  ("SQL is Off");


$car = (isset($_GET['car']) ? $_GET['car'] : null);

mysqli_select_db($db,"car_rental");


$SQL = "SELECT * FROM `products` WHERE name LIKE \'%$car%\'";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

print $db_field['sku'] . "<BR>";
print $db_field['name'] . "<BR>";
print $db_field['img'] . "<BR>";
print $db_field['price'] . "<BR>";

}

?>

This is my current code car variable will change dependent on car selected thanks

5
  • json_encode($db_field);. Commented Jan 20, 2016 at 10:11
  • do i need to echo or print any of the other db_fields? Commented Jan 20, 2016 at 10:15
  • Yup.. echo json_encode($db_field);exit; Commented Jan 20, 2016 at 10:17
  • bro plz mention inside the loop or outside loop @Sougata Commented Jan 20, 2016 at 10:19
  • seem to get this error mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\db\search.php on line 13 Commented Jan 20, 2016 at 10:23

1 Answer 1

6

For getting all values in json format, you need to use like that:

<?
$newArr = array();
while ( $db_field = mysql_fetch_assoc($result) ) {
    $newArr[] = $db_field;
}
echo json_encode($newArr); // get all products in json format.
?>

UPDATE 1:

You are mixing mysqli extension with mysql. Change mysql into mysqli


UPDATE 2:

Why are you connecting database twice in code?

Modified Code:

<?php

$link = mysqli_connect("localhost", "root", "", "car_rental");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$car = (isset($_GET['car']) ? $_GET['car'] : null);

$query = "SELECT * FROM `products` WHERE name LIKE '%$car%'";

if ($result = mysqli_query($link, $query)) {

    $newArr = array();
    /* fetch associative array */
    while ($db_field = mysqli_fetch_assoc($result)) {
        $newArr[] = $db_field;
    }
    echo json_encode($newArr); // get all products in json format.    
}

?>

Side Note:

Also on PHP error reporting in development phase for saving your time.

Sign up to request clarification or add additional context in comments.

10 Comments

get the following error Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\db\search.php on line 13 false
@AhmedBakir: i have changed the variable , try now.
You are mixing mysqli extension with mysql @AhmedBakir
@AhmedBakir: Change mysql into mysqli
thanks it works i wasnt aware that i connected to my DB twice @devpro
|

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.