ive got a script in php which fetched data from server, but i want to encode it in json format.
<?php
// attempt a connection
$dbh = pg_connect("host=10.22.35.11 dbname=iwmp_dev2 user=postgres ");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
// execute query
//$sql = $_POST['pLat'];
$sql = "SELECT officer_name FROM iwmp_officer";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
$array = array();
while ($row = pg_fetch_assoc($result)) {
$i++;
$comm = implode(",",$row);
echo json_encode($comm);
}
// free memory
pg_free_result($result);
// close connection
pg_close($dbh);
?>
but my ouptput is coming in format
"V. M. ARORA""Dr. C. P. REDDY""ARTI CHOWDHARY""JAGDISH SINGH"
also when using implode func on *pgsql_fetch_assoc*, no ","(coma's) are coming.
please help
I think you're doing it in wrong way. Try to do like below.
while ($row = pg_fetch_assoc($result)) {
$rows[] = $row;
}
echo json_encode($rows);
Try this;
while ($row = pg_fetch_assoc($result)) {
echo json_encode($row);
}
You don't need to implode the $row. Try replacing your while loop with below code
while ($row = pg_fetch_assoc($result)) {
echo json_encode($row);
}
Related
How Can I Split This json Object to 3 Part id awnser and type ?
[{"id":"26","answer":[{"option":"3","text":"HIGH"}],"type":"a"},
{"id":"30","answer":[{"option":"3","text":"LOW"}],"type":"b"},
{"id":"31","answer":[{"option":"3","text":"LOW"}],"type":"c"}]
And db Name: array Table Name: user_survey_start JSON Column Name: survey_answers, This is my code:
<?php
$con=mysqli_connect("localhost","root","","arrayy");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers` FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
}
mysqli_close($con);
?>
Try using json_decode()
<?php
$sql="SELECT `survey_answers` FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql))
{
while ($row = mysqli_fetch_row($result))
{
$json = $row[0];
$jason_array = json_decode($json,true);
foreach ($jason_array as $data){
$id[] = $data['id'];
$answer[] = $data['answer'];
$type[] = $data['type'];
// here code to insert/update values to db column
}
echo implode(',',$id);
echo implode(',',$answer);
echo implode(',',$type);
}
}
I'm developing an IOS app, and for the API I'm sending my requests to a URL that should return JSON data with PHP
I am Getting like
[{"Child Care":"After Scool,Breakfast Club\n"},{"Child Care":"Breakfast Club"}]
But I want to get like
[{
"Childcare":[
"All of Childcare",
"After school",
"Breakfast Club"
]}
My code is
<?php
session_start();
$connection=mysqli_connect('localhost','root','','testing') or die(mysqli_error());
$sql="select `Child Care` from Activity_type ";
$result = mysqli_query($connection,$sql) or die("Error in Selecting " . mysqli_error($connection));
$emparray=array();
while($row =mysqli_fetch_assoc($result)){
array_push($emparray,$row);
}
header('Content-Type: application/json');
echo json_encode($emparray);
?>
Simply put it in your JSON array properly.
$emparray = array();
while($row = mysqli_fetch_assoc($result)) {
$emparray['Child Care'][] = $row['Child Care'];
}
header('Content-Type: application/json');
echo json_encode($emparray);
To answer your second question from the comments:
$emparray = array();
while($row = mysqli_fetch_assoc($result)) {
foreach($row as $key => $val) {
$emparray[$key][] = $val;
}
}
header('Content-Type: application/json');
echo json_encode($emparray);
This will give the format you want.Let me know if this works for you.
while($row =mysqli_fetch_assoc($result)){
array_push($emparray,$row['Child Care']);
}
header('Content-Type: application/json');
echo json_encode(array("Childcare" => $emparray));
Try something like,
$sql="select `Child Care` from Activity_type ";
$result = mysqli_query($connection,$sql) or die("Error in Selecting " . mysqli_error($connection));
$emparray=array();
while($row =mysqli_fetch_assoc($result)){
$cares = explode(',' $row['Child Care']);
foreach($cares as $care){
$emparray[] = $care;
}
}
header('Content-Type: application/json');
echo json_encode($emparray);
Use php explode function
I am trying to run a PHP script that uses mysqli library, but it fetches no results on the webhosting server while in my machine running WLAMP PHP 5.14 it works well.
Can you please tell me if the problem is related with mysqli and if there is a way to make it work for my case?.
The lines I am looking to make work look like this:
$sql = "call sp_give_me_data(null,null,null);";
if (!$mysqli->query($sql)) {
die('Invalid query: ' . mysql_error());
} else {
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$datainfo[] = $row;
}
$output = json_encode($datainfo);
echo utf8_encode($output);
} else {
echo "0";
}
}
as I said it fetches "0" records on the hosting which supports PHP 5.3-5.5 and it fetches the correct number of records on my machine. The connection to the remote database (which is the same in both cases) seems successful as well.
Thanks for the insight.
Update
I changed my code so now I am sending the query just once:
mysqli_set_charset($mysqli,"utf8"); //utf-8 accents query
$result = $mysqli->query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$datainfo[] = $row;
}
//utf-8 accents unescaping to be shown correctly
$output = json_encode($datainfo,JSON_UNESCAPED_UNICODE);
echo $output;
} else {
echo "0";
}
}
This works now. Thx!
Your table is empty in the remote server and not empry in local
With the suggestion of #VolkerK, this works now:
mysqli_set_charset($mysqli,"utf8"); //utf-8 accents query
$result = $mysqli->query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$datainfo[] = $row;
}
//utf-8 accents unescaping to be shown correctly
$output = json_encode($datainfo,JSON_UNESCAPED_UNICODE);
echo $output;
} else {
echo "0";
}
}
I have used the below code in my website,
<?php
$con= mysqli_connect("*******","******","*****", "catalejo_articles");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result= mysqli_query($con, "SELECT * FROM baul");
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
mysqli_close($con);
?>
and it is not working. What am i doing wrong??
I am new to php and mysql, any help will be appreciated.
UPDATE:
I want to thank and apologize to all of you who spent precious time trying to help me. I just solved the problem, the original code was OK. The problem was I didn't change the file extension to PHP.
Make sure display error is enabled. add this line at the top of your page and see if it display any error :
error_reporting(-1);
And try inside while loop :
var_dump($row)
Does table contains data?
Try this way,
if ($result = mysqli_query($con, "SELECT * FROM baul", MYSQLI_USE_RESULT)) {
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
mysqli_free_result($result);
}
Check if u have any records in your table using mysqli_num_rows
$con= mysqli_connect("*******","******","*****", "catalejo_articles");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result= mysqli_query($con, "SELECT * FROM baul");
$count = mysqli_num_rows($result);
if($count > 0)
{
while($row = mysqli_fetch_array($result))
{
echo $row['title'] . " " . $row['date'];
}
}else{
echo "No Records found in the table";
}
mysqli_close($con);
From what I can make out of your code, since you were attempting to reference by association instead of by numerical index, you weren't seeing anything because you were missing MYSQLI_ASSOC in your while loop:
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['title']." ".$row['date'];
}
Otherwise you need to know where "title" and "date" columns are located and reference them by numerical value:
while($row = mysqli_fetch_array($result)) {
echo $row[0]." ".$row[1];
}
Alternatively, if mysqli_fetch_array is not working (due to PHP version), try using:
while($row = mysqli_fetch_assoc($result)) {
echo $row['title'].' '.$row['date'];
}
Hiii, I am getting multiple value form database when I am using foreach, plz help
function display($host,$user,$pass,$database)
{
$db = mysql_connect($host, $user, $pass);
mysql_select_db ($database);
$query = "SELECT * FROM `sysdes_moduleinfo`";
$result = mysql_query($query) OR die(mysql_error());
$i=0;
while($row = mysql_fetch_array($result))
{
/*$max = count($row);
while($i<6) {
echo $row[$i]." ";
$i++;
}*/
foreach ($row as $value)
{
//echo $value . " ";
echo htmlspecialchars($value);
}
echo "<br/>";
}
This what I get with this code.
.
This what I have in the database.
The default result type for mysql_fetch_array is to return data in both a number and associative array.
This is why data is duplicated. Try using mysql_fetch_row instead.
However it should be noted that both mysql_fetch_array & mysql_fetch_row are both deprecated.
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach ($row as $value)
{
//echo $value . " ";
echo htmlspecialchars($value);
}
echo "<br/>";
}
mysql_fetch_array returns both a numeric and associative array.