I made a script to display data from online SQL database into JSON format.
The problem is, I don't have the format i was looking for, I get 2 [ more while i wanted only one:
A part of my script:
$sql = "select pseudo, dixsec from user;";
$result = mysqli_query($conn,$sql);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
$arrray = array("server_response" => array($rows));
print json_encode($arrray);
What i get (You can see here that i have 2 "["):
Json i get
How can i solve it and get only one "[" ?
$rows is already an array.
Try with: $arrray = array("server_response" => $rows);
Related
I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}
I'm trying to fetch data from database, date-with-time(timestamp) and values(number) , and storing into an array using php.
here is my data in database as follows=
WD DT
25-FEB-15 12.14.00.000000 AM 15.739993
25-FEB-15 12.23.00.000000 AM 13.698263
25-FEB-15 12.43.00.000000 AM 13.214383
fetch.php
<?php
include("md.php");
$sql = "SELECT * from datatable";
$result =oci_parse($conn, $sql);
$r=oci_execute($result);
$arr = array();
$row=oci_num_rows($stid);
$arr[0]=array('wd','dt');
for($i=1; $i<($row+1); $i++)
{
$arr[$i]= array(substr(oci_result($result, $i-1, "wd"),0,18),(float)oci_result($result,$i-1,"dt"));
//$arr[$i]= array(substr(oci_result($result, $i-1, "wd"),0,18),(int)oci_result($result,$i-1,"dt"));
}
echo json_encode($arr);
//print_r($arr);
?>
$arr getting following output:
[["WD"],["DT"]]
Q1. Why am i not getting rest of data? where am i doing wrong?
but if i use
while($row = oci_fetch_row($stid)){
$arr[] = $row;
}
if i use json_encode then =
[["25-FEB-15 12.14.00.000000 AM","15.739993"],["25-FEB-15 12.23.00.000000 AM","13.698263"],["25-FEB-15 12.43.00.000000 AM","13.214383"],....
if i use
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = $row;
}
if i use json_encode then =
[{"WD":"25-FEB-15 12.14.00.000000 AM","DT":"15.739993"},{"WD":"25-FEB-15 12.23.00.000000 AM","DT":"13.698263"},........]
I want the output as follows=
[["25-FEB-15 12.14.00 AM",15.739993],["25-FEB-15 12.23.00 AM",13.698263],["25-FEB-15 12.43.00 AM",13.214383],....]
Q2. How can i get it?
please help
Because the data is being returned in an associative array you get the column name and the data for each column in each row returned to you. So this is being returned in $row
"WD" => "25-FEB-15 12.14.00.000000 AM", "DT" => "15.739993"
All you need to do is pick out the data from each row and ignore the Key like so :-
$arr = array();
while($row = oci_fetch_array($stid,OCI_ASSOC)){
$arr[] = array($row['WD'] , $row['DT']);
}
echo json_encode($arr);
I have a few queries that I'm executing in my Android app. Each will only return one row of data, so I'd like to treat the output as JSONObjects rather than JSONArrays since they would be just be arrays with single objects inside; kind of pointless in my view.
As of now my PHP looks like this:
$query = "SELECT moveCount FROM Chessmates.Board_States WHERE Games_GameID = 2;";
$sth = mysqli_query($con, $query);
if(mysqli_errno()) {
echo "error";
} else {
$rows = array();
while($r = mysqli_fetch_array($sth, MYSQLI_ASSOC)) {
$rows[] = $r;
//var_dump($r);
}
echo json_encode($rows);
}
and the output looks like this:
[{"moveCount":"0"}]
I'd like it to look like this:
{"moveCount":"0"}
If it's only returning one row of data, then you don't need to make an array.
while($r = mysqli_fetch_array($sth, MYSQLI_ASSOC)) {
$rows = $r;
}
You get the data as you did because you were creating an array of objects, But since you're only going to ever get one result (as you stated), you can simple set it up as a variable.
I'm trying to put data from my database into seperate arrays within another array. This works but when I'm trying to fetch the 'user_id' information, it only shows one number so it works like a string. How can I get it to work like an array and get the entire user_id?
$fetch = mysqli_query($con, "SELECT * FROM spotify_userdata");
$return_arr = [];
while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
$return_arr[] = array(
$row_array['user_id'] = $row['user_id'],
$row_array['name'] = $row['name'],
$row_array['artists'] = $row['artists'],
);
}
$user = json_encode($return_arr[0]);
echo $user[2];
This code returns 1 so it show the third number of the user_id. How can I get it to show the entire user_id like this: 111434343
You have many things in your code that's wrong:
Remove the last array item's comma
Change
$return_arr = [];
To
$return_arr = array();
3.Add:
$row_array = array()
at the begginning of all that code
At the end your code must be like this:
$fetch = mysqli_query($con, "SELECT * FROM spotify_userdata");
$row_array = array();
while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
$return_arr = array(
$row_array['user_id'] = $row['user_id'],
$row_array['name'] = $row['name'],
$row_array['artists'] = $row['artists'],
);
}
$user = json_encode($return_arr[0]);
According to your code you should use:
echo $user['user_id'];
But the real problem is - where is $row_array initialized?!?
And even bigger problem - why use "=" inside array creation in the while ... it seems to me that "=>" would fit better, don't you think?
I've got a database with 5 columns and multiple rows. I want to fetch the first 3 rows and echo them as an array. So far I can only get the first row (I'm new to PHP and mysql). Here's my PHP so far:
//==== FETCH DATA
$result = mysql_query("SELECT * FROM $tableName");
$array = mysql_fetch_row($result);
//==== ECHO AS JSON
echo json_encode($array);
Help would be much appreciated.
You need to loop through the results. mysql_fetch_row gets them one at a time.
http://php.net/manual/en/function.mysql-fetch-row.php
The code would end up like:
$jsonData = array();
while ($array = mysql_fetch_row($result)) {
$jsonData[] = $array;
}
echo json_encode($jsonData);
//json_encode()
PLEASE NOTE
The mysql extension is deprecated in PHP 5.5, as stated in the comments you should use mysqli or PDO. You would just substitute mysqli_fetch_row in the code above.
http://www.php.net/manual/en/mysqli-result.fetch-row.php
I do like this while quering an ODBC database connection with PHP 5.5.7, the results will be in JSON format:
$conn = odbc_connect($odbc_name, 'user', 'pass');
$result = odbc_exec($conn, $sql_query);
Fetching results allowing edit on fields:
while( $row = odbc_fetch_array($result) ) {
$json['field_1'] = $row['field_1'];
$json['field_2'] = $row['field_2'];
$json['field_3'] = $row['field_1'] + $row['field_2'];
array_push($response, $json);
}
Or if i do not want to change anything i could simplify like this:
while ($array = odbc_fetch_array($result)) { $response[] = $array; }
What if i want to return the results in JSON format?, easy:
echo json_encode($response, true);
You can change odbc_fetch_array for mysqli_fetch_array to query a MySql db.
According to the PHP Documentation mysql_fetch_row (besides that it's deprecated and you should use mysqli or PDO)
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
so you need for example a while loop to fetch all rows:
$rows = array();
while ($row = mysql_fetch_row($result)) {
$rows[] = $row;
}
echo json_encode($rows);
I leave it to you how to only fetch 3 rows :)
You need to put this in some kind of a loop, mysql_fetch_row returns results one at a time.
See example:
http://www.php.net/manual/en/mysqli-result.fetch-row.php#example-1794
$result = mysql_query( "SELECT * FROM $tableName ORDER BY id LIMIT 3");
$json = array();
while($array = mysql_fetch_row($result)){
$json[] = $array;
}
echo json_encode($json);