1

My mind got stuck when i try to develope this: i have a table in my database called "article" whit two column, "name" and "price". How can i extract all rows from my table and echo all column in JSON? i really can't understand how to convert result in JSON. My mind it's stuck like never before. i need to echo something like this:

{"items": {
    "items":[
        {"name": "firstitemname", 
         "price": "5"
        },
        {"name": "secondone", 
         "years": "3"
        }],
}}

Please help me fixing my buggy code!

<?php

$query = mysql_query("SELECT * FROM itemlist");
$nameitem = array();
$itemprice = array();
while($row = mysql_fetch_array($query)){
array_push($nameitem , $row['nome']);
array_push($itemprice, $row['pix']);
}

?>

4 Answers 4

4

You would simply edit your PHP as follows.

<?php

$query = mysql_query("SELECT * FROM itemlist");
$items = array();
while($row = mysql_fetch_array($query)){
$items[] = array('name' => $row['nome'], 'price' => $row['pix']);
}

echo json_encode(array('items'=>$items));

?>    

http://php.net/manual/en/function.json-encode.php

JSON is super easy to deal with in PHP.

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

1 Comment

Let me know if it works as expected. Also, I spelled name the same way you did, not sure if that is valid.
2

If you're using PHP 5.2 or greater, you can use the json_encode function to do exactly what you're trying to do: http://www.php.net/manual/en/function.json-encode.php

For your code, you should be able to do something like this:

$query = mysql_query("SELECT * FROM itemlist");
$json_output = array();
while($row = mysql_fetch_assoc($query)){
    $json_output[] = json_encode($row);
}

Here, $json_output will contain an array of strings with the json encoded string of each row as each array element. You can output these as you please.

Comments

2
<?php
$result = mysql_query("select * from item_list");
$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
print json_encode($rows);
?>

Comments

0

Convert Data table to json with following code :

echo(json_encode($array));  

for example ( select data from mysql and convert to json ):

 public function SELECT($tableName,$conditions){

      $connection = mysqli_connect($hostname, $userName, $password,$dbName);
      try {

        if (!$connection)
            die("Connection failed: " . $connection->connect_error);
        else
        {
            $qry = "";
            if(!$this->IsNullOrEmptyString($conditions))
               $qry = "SELECT * FROM `".$tableName."` WHERE ".$conditions;
            else
               $qry = "SELECT * FROM `".$tableName."`";

            $result = mysqli_query( $connection, $qry);
            if($result) {
                $emparray = array();
                while($row =mysqli_fetch_assoc($result))
                    $emparray[] = $row;

                echo(json_encode($emparray));           
            }
            else
                echo(mysqli_error($connection));       
            } 
            mysqli_close($connection); 
      } catch(Exception $ex) {
          mysqli_close($connection);
          echo($ex->getMessage());
      }  
 }

Comments

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.