I've searched through the stack for an answer but it seems that I cannot find a solution for the following code:
$servers = array();
while($row = mysql_fetch_array( $result ))
{
$servers[] = $row['name'] => array('ip' => $row['ip'],'port' => $row['port'],'info' => $row['info'],'purpose' => $row['purpose']),
}
What I'm trying to achieve is fetch all results but I receive the following error:
syntax error, unexpected '=>' (T_DOUBLE_ARROW).
Making it like this gets only the last item:
while($row = mysql_fetch_array( $result )) {
$servers = array(
$row['name'] => array('ip' => $row['ip'],'port' => $row['port'],'info' => $row['info'],'purpose' => $row['purpose']),
);
}
That being said, $servers is in the while loop, thus making it index only the last item. I don't know what causes the unexpected => :/
It looks like you're trying to do this:
$servers = [];
while($row = mysql_fetch_array( $result )) {
$servers[$row['name']] = [
'ip' => $row['ip'],
'port' => $row['port'],
'info' => $row['info'],
'purpose' => $row['purpose']
];
}
That gives you a final $servers array, indexed by server name, with an array of details.
Side note: as Jay commented, you really shouldn't be using mysql functions any longer, really encourage you to check out mysqli or PDO functions instead.
Related
I am trying to make a dynamic JSON array in PHP, however when I try to do so it returns "Array". Here is the code I am currently using:
<?php
require '../../scripts/connect.php';
$array = '';
if($result = $db->query("SELECT * FROM art") or die ($db->error)){
if($count = $result->num_rows) {
while($row = $result->fetch_object()){
$array .= array(
'title' => $row->title,
'image' => "http://www.thewebsite.com/img/2.jpg",
'rating' => 7.7,
'releaseYear' => 2003,
'genre' => array(
'0' => $row->category,
'1' => $row->subcategory
)
);
}
}
}
echo json_encode($array);
?>
Can anyone suggest how I might go about fixing this?
And if anyone has suggestions about creating a dynamic JSON array, some help would be much appreciated.
Change your declaration of $array to be an array:
$array = array();
Then in your while loop, when you add the new array to $array, push it like this:
$array[] = array('title'=>$row->title, etc...)
Output Array (The variable name is $r)
Array
(
[Jan-14] => 7588793.52
[Feb-14] => 9944970.87
[Mar-14] => 8567790.20
[Apr-14] =>
[May-14] =>
[Jun-14] =>
[Jul-14] =>
[Aug-14] =>
[Sep-14] =>
[Oct-14] =>
[Nov-14] =>
[Dec-14] =>
)
What I have done so far is..
while($r = mysql_fetch_assoc($query)) {
$series1['data'][] = $r['Jan-14'];
$series2['data'][] = $r['Feb-14'];
.... it will go till Dec-14.......
array_push($result,$series1);
array_push($result,$series2);
.... it will go till Dec-14.......
}
Expected Output:
The code should look something like this this (dynamic)
while($r = mysql_fetch_assoc($query)) {
for($i=1;$i<=count($r);$i++){
$series.$i['data'][] = ??????????
array_push($result,$series.$i);
..................
}
}
Help me out. Don't talk about the db structure or normalization. The db was given by my client.
Thanks,
Kimz
Do you mean like this?
$result = array();
foreach($r as $month => $value) {
$result[] = array('data' => array($value));
}
I am trying to create a JSON array with the information selected from a database but I cannot give the array a name.
while($row = mysql_fetch_array($result))
{
$arr = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
echo json_encode($arr);
}
I want to see the result;
{"events":[{"isim":"eere","yer":"dddd","top":"asdfsdffgdfgdfg","tar":"2013-10-18","saat":"12:46"}{"isim":"fhjfr","yer":"yhjrhj","top":"ryjryjrj","tar":"2013-10-30","saat":"12:45"}{"isim":"sfsgsg","yer":"sfgssfg","top":"sgsfgsg","tar":"2013-10-31","saat":"12:45"}]}
But I cannot see the
{"events":[
in the beggining and
]}
at the end.
Thank you.
To generate valid JSON, you first need to add everything to a multi-dimensional array and only then when that is complete, encode it:
$arr = array();
while($row = mysql_fetch_array($result))
{
$arr[] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
// or perhaps just: $arr[] = $row;
}
echo json_encode($arr);
Also note that the mysql_* functions are deprecated.
To put everything under an events key, you would need something like:
$arr['events'][] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
So I'm using x-editable for bootstrap, which is awsome for me.
With that, to return a menu for selection one needs to return an array as such to make it work:
$arr = array(
array('value' => 'Male', 'text' => 'Male'),
array('value' => 'Female', 'text' => 'Female'),
);
It's fine if you have to write it, but now I need to make this array from database output.
So for instance if I run a "while($row" loop, how is the output going to be an array like that. This is what I'm trying, but oviously failing at cause this is not working:
$query = 'SELECT id,app_name FROM apps';
$result = mysql_query($query) or bomb($s,509,addslashes($query),addslashes(mysql_error()));
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
$arr .= Array(
Array('value' => $row['id'], 'text' => $row['app_name']),
);
}
}
It's probably a silly question, so thanks in advanced.
Arrays can't be concatenated like strings.
Try something like this:
while($row = mysql_fetch_assoc($result)) {
$arr[]=Array('value' => $row['id'], 'text' => $row['app_name']);
}
I have been looking around for PHP tutorials and I found a very detailed one that have been useful to me.
But now, I have a question. The results showed by the API are stored into a $results array. This is the code (for instance):
$fetch = mysql_fetch_row($go);
$return = Array($fetch[0],$fetch[1]);
$results = Array(
'news' => Array (
'id' => $return[0],
'title' => $return[1]
));
My question is.. I want to display the last 10 news.. how do I do this? On normal PHP / mySQL it can be done as:
while($var = mysql_fetch_array($table)) {
echo "do something"
}
How can I make it so the $results can print multiple results?
I have tried like:
while($var = mysql_fetch_array($table)) {
$results = Array(
'news' => Array (
'id' => $return[0],
'title' => $return[1]
));
}
But this only shows me one result. If I change the $results .= Array(...) it gives error.
What can I do?
Thanks!
Edit
My function to read it doesn't read when I put it the suggested way:
function write(XMLWriter $xml, $data){
foreach($data as $key => $value){
if(is_array($value)){
$xml->startElement($key);
write($xml, $value);
$xml->endElement();
continue;
}
$xml->writeElement($key, $value);
}
}
write($xml, $results);
$results[] = Array(
'news' => Array (
'id' => $return[0],
'title' => $return[1]
));
That should do it.
If you are familiar with arrays, this is the equivalent of an array_push();
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:
<?php
$array[] = $var;
?>
Use [] to add elements to an array:
$results = array();
while($var = mysql_fetch_array($table)) {
$results[] = Array(
'news' => Array (
'id' => $var[0],
'title' => $var[1]
));
}
}
You can then loop through the $results array. It's not an optimal structure but you should get the hang of it with this.