I must put to json_encode all values of specifed mysql table column
$fromdate = $_GET['fromdate'];
$getrezhiredh = safe_query("
SELECT rezhour FROM rezhiredhours
WHERE rezdate = '".$fromdate."' ORDER BY rezhour
");
$rows = array();
while($r = mysql_fetch_assoc($getrezhiredh)) {
$rows[] = $r;
}
print json_encode($rows);
With code above i have one problem. This code return result only when in table we have one row with selected data. In this case json_encode() result is
[{"rezhour":"1"}]
But when table have more than one row with selected data result don't return anything but
[]
How put to json_encode() all values selected from table?
EDIT:
I just wonder why in case when we have more rows in table with selected data, result don't give as example below
[{"rezhour": { [0] => "1",[1] => "4" }]
Instead in result we have "[]"
Thank You in advance.
try changing
mysql_fetch_assoc($getrezhiredh)
to
mysql_fetch_object($getrezhiredh)
Results from mysql_fetch_assoc are different than you think. Each row is more or less like this:
array(1)
"rezhour" => "1"
So you access the data it like this:
while($r = mysql_fetch_assoc($getrezhiredh)) {
$rows[] = $r["rezhour"];
}
Related
I have a mysqli resultset with two columns of data and several rows. I want to store each row of the resultset as an indexed subarray in my result array (specifically in $rows['data']).
This is my current code:
$query = mysqli_query($con,"SELECT Energy_UTC,Total_watts FROM combined_readings");
$rows = array();
$rows['name'] = 'Total_watts';
while ($tmp = mysqli_fetch_array($query)) {
$rows['data'][] = $tmp['Energy_UTC'];
$rows['data'][] = $tmp['Total_watts'];
}
This results in an array that looks like this:
{"name":"Total_watts","data":[1519334969,259,1519335149,246,1519335329,589,1519335509,589,1519335689,341,1519335869,341,1519336050,523,1519336230,662,1519336410,662,1519336590,469]}
But I need the result to be an array that looks like this:
{"name":"Total_watts","data":[1519334969,259],[1519335149,246],[1519335329,589],[1519335509,589],[1519335689,341],[1519335869,341],[1519336050,523],[1519336230,662],[1519336410,662],[1519336590,469]}
Can someone suggest a change in the PHP while loop to produce this output?
You just need to adjust your syntax to place the elements in the same subarray.
while($tmp = mysqli_fetch_array($query)) {
$rows['data'][] = [$tmp['Energy_UTC'],$tmp['Total_watts']];
}
p.s. Additionally, you could use mysqli_fetch_assoc() since you are only accessing the associative keys. Or even better, use mysqli_fetch_row() and assign the row to your result array.
All baked, it could look like this:
if(!$result=mysqli_query($con,"SELECT Energy_UTC,Total_watts FROM combined_readings")){
// handle the query error
}else{
$rows=['name'=>'Total_watts'];
while($row=mysqli_fetch_row($result)){
$rows['data'][]=$row; // this will store subarrays like: [1519334969,259]
}
}
I am pulling tables from a database and I'm trying to keep the userdata table from being included in the array that is eventually sent to my application. I've tried using array_diff and unset, but I haven't had any luck with it. As tables can be added, the userdata one won't always be in the same position.
The array when I check on JSONlint.com appears like this:
[{"table_name": "OSA"
}, {
"table_name": "OSB"
}, {
"table_name": "userdata"
}]
code in PHP file:
$tables = array();
while ($row = mysqli_fetch_array($result)){
array_push($tables, array("table_name"=>$row[0]));
}
$remove= array({"table_name": "userdata"});
$result = array_diff($tables, $remove);
echo json_encode($result);
The optimal way would probably be to exclude it in the query:
SELECT column FROM table WHERE column <> 'userdata'
Or, just don't add it to the array:
while($row = mysqli_fetch_array($result)) {
if($row[0] != 'userdata') {
$tables[] = array('table_name' => $row[0]);
}
}
Your array_push should work fine but I prefer the above.
But to answer the unset question. Just get the table_data column values, search for userdata and unset that key:
unset($tables[array_search('userdata', array_column($tables, 'table_name'))]);
I am trying to return a json from a MySQL query to use it with xCode but I cannot get an array of several objects with multiple fields. I have read the documentation on php.net and over here, but I still can't get it.
1) Let's say I have a MySQL table with 3 rows (for example 3 people). Each row contains 3 fields (lastname, firstname, dateOfBirth):
$result = mysqli_query($mysqli, $sql) // where $sql = "SELECT * FROM tbl_syncList"
$resultArray = array();
while ($row = $result->fetch_array()) {
$resultArray[] = $row["lastname"];
}
echo json_encode($resultArray); // --> return "["Lastname1", "Lastname2"...] - that's okay, I understand I return the value of the key "lastname"
I don't know how to get an array with the entire rows (all the fields at once), like:
[["Firstname1", "Lastname1", "dateOfBirth1"], ["Firstname2", "Lastname2", "dateOfBirth2"],...]
I tried to replace
$resultArray[] = $row["lastname"];
with:
$resultArray[] = $row;
but it just gives the world...
"array"
...with no content. Does anyone have an idea?
Thanks a lot!
Are you sure the array is properly populated to begin with? print_r($resultArray) and see what you have there with = $row (which is correct).
I have a simple query with outputs only one column (rows number may change)
SELECT column1 as NUMBER WHERE column1 ...
Output,
NUMBER
1
2
I just want it as an array in PHP with them as comma seperated.
So I did,
$rows = array();
while ($row = mysql_fetch_row($result)) {
$rows[] = $row;
and its working if I echo it with print_r
However because of some reason I cant get the plain values out of this array.
echo $rows[0] gives me the word 'Array'
echo implode(",", $rows); gives me 1Array',Array1
I tried
echo json_encode($rows[0]);
gives me
["1"]
I simply want 1,2
After a lot of different tries I gave up and added group_concat in the sql query. I just want to know what I did wrong.
Thanks.
try this
SELECT GROUP_CONCAT(`column1` SEPARATOR ',') AS number FROM table where....
mysql_fetch_row i returning a single element array. To have your desired output, only select the 1st element:
$rows = array();
while ($row = mysql_fetch_row($result)) {
$rows[] = $row[0];
foreach($rows as $key => $value){
}
should do the trick, with an echo it will output the results as an associative array
just make a simple amendment to your code:
$rows[] = $row;
into:
$rows[] = $row[0];
and use:
implode(", ", $rows[]);
How can i get every row of a mysql table and put it in a php array? Do i need a multidimensional array for this? The purpose of all this is to display some points on a google map later on.
You need to get all the data that you want from the table. Something like this would work:
$SQLCommand = "SELECT someFieldName FROM yourTableName";
This line goes into your table and gets the data in 'someFieldName' from your table. You can add more field names where 'someFieldName' if you want to get more than one column.
$result = mysql_query($SQLCommand); // This line executes the MySQL query that you typed above
$yourArray = array(); // make a new array to hold all your data
$index = 0;
while($row = mysql_fetch_assoc($result)){ // loop to store the data in an associative array.
$yourArray[$index] = $row;
$index++;
}
The above loop goes through each row and stores it as an element in the new array you had made. Then you can do whatever you want with that info, like print it out to the screen:
echo $row[theRowYouWant][someFieldName];
So if $theRowYouWant is equal to 4, it would be the data(in this case, 'someFieldName') from the 5th row(remember, rows start at 0!).
$sql = "SELECT field1, field2, field3, .... FROM sometable";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
echo $array[1]['field2']; // display field2 value from 2nd row of result set.
The other answers do work - however OP asked for all rows and if ALL fields are wanted as well it would much nicer to leave it generic instead of having to update the php when the database changes
$query="SELECT * FROM table_name";
Also to this point returning the data can be left generic too - I really like the JSON format as it will dynamically update, and can be easily extracted from any source.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo json_encode($row);
}
You can do it without a loop. Just use the fetch_all command
$sql = 'SELECT someFieldName FROM yourTableName';
$result = $db->query($sql);
$allRows = $result->fetch_all();
HERE IS YOUR CODE, USE IT. IT IS TESTED.
$select=" YOUR SQL QUERY GOOES HERE";
$queryResult= mysql_query($select);
//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();
//STORE ALL THE RECORD SETS IN THAT ARRAY
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC))
{
array_push($data_array,$row);
}
mysql_free_result($queryResult);
//TEST TO SEE THE RESULT OF THE ARRAY
echo '<pre>';
print_r($data_array);
echo '</pre>';
THANKS