I am getting back two rows from my query, I tested it in phpadmin.
In firebug I can only see the data from one row.
What could be wrong that I don't see?
$data = mysql_fetch_assoc($r);
}
}
header('Content-type: application/json');
$output = array(
"check" => $check,
"users" => $data,
"testnumberoffrows" => $number
);
echo json_encode($output);
in the ajaxfunction
if( data.check ){
var user = data.users;
console.log(user);
thanks, Richard
mysql_fetch_assoc() fetches only one row. You need to loop until it returns FALSE, building up an output array.
Something like this:
while (($row = mysql_fetch_assoc($r)) !== FALSE) {
$data[] = $row;
}
Please try
$got=array();
while ($row = mysql_fetch_array($r)) {
array_push($got, $row);
}
mysql_free_result($r);
header('Content-type: application/json');
$output = array(
"check" => $check,
"users" => $data,
"testnumberoffrows" => $number
);
echo json_encode($output);
Related
I have a bit of code I would like to pass through my json array that is not contained in the database $row. So i tried to set a custom variable and that didn't work. Is there a way to do this so the below content gets sent through with the array?
Here is what I tried
PHP
if ($photo_numff == 1) {
$streamitem_uploadimage_count =" Uploaded new image";
} else if($photo_numff > 1) {
$streamitem_uploadimage_count =" Uploaded ".$photo_numff." new images";
} else {
}
JSON array
$rowcount = mysqli_num_rows($result);
$json = array(
'posts' => array(),
'count' => $rowcount
);
while ($row = mysqli_fetch_array($result)) {
$posts[] = array(
//Post information and ids
'streamitem_id' => $row['streamitem_id'], // post id
'streamitem_uploadimage_count' => $streamitem_uploadimage_count,
);
$rowcount++;
}
$json['posts'] = $posts;
echo json_encode($json);
I can then take this through my ajax "+response['streamitem_uploadimage_count']+"
I have also tried array_push($json['posts'], array( 'streamitem_uploadimage_count' => $streamitem_uploadimage_count, ) );but doesn't work
I have now got this working by adding
'streamitem_uploadimage_count' => $streamitem_uploadimage_count
within the $Json array also.
Looking for some help.. Don't know the best approach to this issue...
I'm pushing a new reference onto an array but the "true" value is being inserted with quotes, which fails my json format.
while( $row = mysqli_fetch_assoc($res) ) {
if($row['id']=="2"){
$row['children']= 'true';
}
$data[] = $row;
}
echo json_encode( $data);
Outputs
[{"id":"2","name":"john","text":"john","parent_id":"0","children":"true"}]
When i need...
{"id":"2","name":"john","text":"john","parent_id":"0","children":true}]
How would i go about removing the qoutes or inserting it correctly first.??
If you want the 'children' to be boolean then set it to boolean.
while( $row = mysqli_fetch_assoc($res) ) {
if($row['id']=="2"){
$row['children'] = true;
}
$data[] = $row;
}
echo json_encode( $data);
I would like to say thank you for reading this question.
And my question is. I have this php code with sql query:
mysql_connect($mysql_server, $mysql_login, $mysql_password);
mysql_select_db($mysql_database);
$req = "SELECT name, elements "
."FROM lwzax_zoo_item "
."WHERE application_id = '2' AND elements LIKE '%".$_REQUEST['term']."%' ";
$query = mysql_query($req);
while($row = mysql_fetch_array($query))
{
$results[] = array('label' => $row['name'], 'desc' => $row['elements']);
}
$json = json_encode($results);
echo $json;
And output is:
[
{
"label":"0146T",
"desc":" {\n\t\"cec36dd6-ffde-494d-b25c-8e58bff84e22\": {\n\t\t\"0\": {\n\t\t\t\"value\": \"Ccta W\\/Wo Dye\"\n\t\t}\n\t}\n}"
},
{
"label":"64653",
"desc":" {\n\t\"cec36dd6-ffde-494d-b25c-8e58bff84e22\": {\n\t\t\"0\": {\n\t\t\t\"value\": \"Chemodenervation Eccrine Glands Oth Area Per Day\"\n\t\t}\n\t}\n}"
}
]
But I need only label data and value data...so it should look like:
[
{
"label":"0146T",
"desc":"Ccta W\\/Wo Dye"
},
{
"label":"64653",
"desc":"Chemodenervation Eccrine Glands Oth Area Per Day"
}
]
Could you please help me?
Thank you very much for help
UPDATE: Deleted $b = json_decode($row['desc'], true); as it wasn't used, just a junk from all my attempts to succeed.
You're decoding the JSON and assigning it to $b, but you're not doing anything with that variable. Use:
$results[] = array('label' => $row['name'],
'desc' => $b['cec36dd6-ffde-494d-b25c-8e58bff84e22'][0]['value']);
Also, you need to give a second argument to json_decode, so it will return an associative array rather than an object.
$b = json_decode($row['elements'], true);
OK, well, first things first, initialize your array OUTSIDE your loop.
while($row = mysql_fetch_array($query))
{
$b = json_decode($row['elements']);
$results[] = array('label' => $row['name'], 'desc' => $row['elements']);
}
Then you should probably do this:
$results = array();
while($row = mysql_fetch_array($query))
{
$b = json_decode($row['elements']);
array_push($results, array('label' => $row['name'], 'desc' => json_decode($row['elements'], true));
}
The at the end
$json = json_encode($results);
echo $json;
See if that helps.
I'm trying to pull data from my database using json in php. I have a few elements I need to specific then to post them on a page.
I want to "fetch" the data from mysql and return it to a json_encode. How can I do this using the SELECT method. Some had used PDO methods and other have used mysql_assoc, which confuses me.
For instance,
I have rows of: 'id' , 'title' , 'start', 'backgroundColor'...etc. along with a default value for all of them. ($array[] = "someValue = default")
I want it to export like so:
array(
'id' => 1,
'title' => "someTitle",
'start' => "2012-04-16",
'backgroundColor' => "blue",
'someValue' = > "default",
...
), ....
));
If anyone could help me with this with the best detail, I'd be awesome!
If you wanted to do this with PDO then here is an example:
<?php
$dbh = new PDO("mysql:host=localhost;dbname=DBNAME", $username, $password);
$sql = "SELECT `id`, `title`, `time`, `start`, `backgroundColor`
FROM my_table";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
//To output as-is json data result
//header('Content-type: application/json');
//echo json_encode($result);
//Or if you need to edit/manipulate the result before output
$return = [];
foreach ($result as $row) {
$return[] = [
'id' => $row['id'],
'title' => $row['title'],
'start' => $row['start'].' '.$row['time'],
'backgroundColor' => $row['backgroundColor']
];
}
$dbh = null;
header('Content-type: application/json');
echo json_encode($return);
?>
You don't "fetch to a json array".
You fetch your database results into a PHP array, then convert that php array, AFTER THE FETCHING IS COMPLETED, to a json string.
e.g.
$data = array();
while ($row = mysql_fetch_assoc($results)) {
$data[] = $row;
}
echo json_encode($data);
You can get the result from mysql,then format it to json
$array = array();
while($row = mysqli_fetch_array($result))
{
array_push($array,$row);
}
$json_array = json_encode($array);
Please check for SELECT methods here
In general it would look like this
$data = array(); // result variable
$i=0
$query = "SELECT id,title,start,backgroundColor FROM my_table"; // query with SELECT
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){ // iterate over results
$data['item'][$i]['id'] = $row['id']; // rest similarly
...
...
$i++;
}
header('Content-type: application/json'); // display result JSON format
echo json_encode(array(
'success' => true,
'data' => $data // this is your data variable
));
$responses = array();
while ($row = mysql_fetch_array($result)) {
$response = array(
'name' => $row['name']
);
$row;
$responses['name5'] = $response;
}
echo json_encode($responses);
I'm currently only getting 1 rows from this statement I know for a fact their are more.
On each iteration of your while loop, you are overwriting the same array key $responses['name5'], so in the end you'll only have one value in the $responses array.
Instead, you might want something like this to append to the end of the array:
$responses[] = $response;
you are overwriting the $response variable that's why, array_push instead
$responses['name5'] = $response;
You will get only last row because you replace your data each cycle step.
Try this:
$responses['name5'][] = $response;
Because you're resetting the $response array to a single array in the loop. You want to add to the array.
$responses = array();
while ($row = mysql_fetch_array($result)) {
array_push($response, array(
'name' => $row['name']
));
$row;
$responses['name5'] = $response;
}
echo json_encode($responses);
do
$responses[] = array('name5' => $response);