Split a Column of json Object to More Column via PHP - php

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);
}
}

Related

Mysql delete a row php

I have a MySQL table and i would like to delete a row in my table and after deleting the row the result must show all data left in my table .
<?php
include 'Connection.php';
// Create connection
$con= mysqli_connect($host,$user,$pass,$db);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT * FROM productos";
$result = $con->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc())
{
$tem = $row;
$json = json_encode(array("productos"=>$tem));
}
} else {
echo "No Results Found.";
}
echo $json;
$con->close();
?>
This is my select code... now i don't know how create the delete function..
Any help please.
You are overwritting, make array of rows and then echo json_encode at last
$items = array();
if ($result->num_rows >0) {
while($row = $result->fetch_assoc())
{
$items[] = $row;
}
} else {
echo "No Results Found.";
}
echo json_encode(array("productos"=>$items));
$con->close();

Parsing json array And Merging Objects via php

I Have a json array with id type answer I'm merging id, type and answer together and put them in the id2 column, so why can't I get $answers value in output?
[{"id":"38","answer":[{"option":"3","text":"HIGH"}],"type":"a"},
{"id":"39","answer":[{"option":"3","text":"LOW"}],"type":"b"},
{"id":"40","answer":["Hello Word"],"type":"c"}]
This is my code:
<?php
$con=mysqli_connect("localhost","root","","arrayok");
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
if(!is_null($json)){
$json = preg_replace("!\r?\n!", "", $json);
$jason_array = json_decode($json,true);
// id2
$id = array();
foreach ($jason_array as $data) {
if (array_key_exists('id', $data)) {
if (array_key_exists('type', $data)) {
if (array_key_exists('answer', $data)) {
foreach($data['answer'] as $ans){
$answers[] = isset($ans['text']) ? $ans['text'] : $ans;
}
$id[] = ' ID='.$data['id'].', TYPE='.$data['type'].', AWNSER='.$answers;
}
}
}
}
// lets check first your $types variable has value or not?
$ids= implode(',',$id); /// implode yes if you got values
$sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql
echo $sql1."<br>";
mysqli_query($con,$sql1);
}
}
}
mysqli_close($con);
?>
And this is my output:
update user_survey_start set id2=' ID=38, TYPE=a, AWNSER=Array,
and I got Notice: Array to string conversion in C:\wamp64\www\json\awnser.php on line 29
I want to have value of $answers
Solved By Myself
Because I Couldn't Put Awnser Directly To id[], I Taked Awnser Like This:
$id[] = ' ID='.$data['id'].', TYPE='.$data['type'];
$id[] = isset($ans['text']) ? ' AWNSER='.$ans['text'] : ' AWNSER='.$ans;
And This is My Code:
<?php
$con=mysqli_connect("localhost","root","","arrayok");
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
if(!is_null($json)){
$json = preg_replace("!\r?\n!", "", $json);
$jason_array = json_decode($json,true);
// id2
$id = array();
foreach ($jason_array as $data) {
if (array_key_exists('id', $data)) {
if (array_key_exists('type', $data)) {
if (array_key_exists('answer', $data)) {
foreach($data['answer'] as $ans){
$id[] = ' ID='.$data['id'].', TYPE='.$data['type'];
$id[] = isset($ans['text']) ? ' AWNSER='.$ans['text'] : ' AWNSER='.$ans;
}
}
}
}
}
// lets check first your $types variable has value or not?
$ids= implode(',',$id); /// implode yes if you got values
$sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql
echo $sql1."<br>";
mysqli_query($con,$sql1);
}
}
}
mysqli_close($con);
?>

MYSQL foreach Row Echo Data

How do I echo out every column's data of a row from MYSQL results?
I do not know what the rows are as the query is dynamically created.
Here's what I have:
$query = $_POST['query'];
// Create connection
$con=mysqli_connect($db_host, $db_user, $db_pass, $db_name);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($results)) {
}
$row is just an array. Like any other array you can do fun things like iterate over it:
while ($row = mysqli_fetch_array($results)) {
foreach ($row as $key => $value) {
echo 'Key: ' . $key . ', Value: ' . $value;
}
echo "<br><br>\n"
}

trouble retrieving data from sql database with php

I'm pretty new to php, and I'm teaching myself. I've looked at a few different resources, and the php script I have now doesn't return any critical errors when executed, but its not returning the data from the table.
<?php
$connect = mysqli_connect("localhost","*","*","*");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
$fetch = mysqli_fetch_array($rs);
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
echo $fetch;
mysqli_close($connect);
echo "hello";
?>
you have double entry:
$fetch = mysqli_fetch_array($rs); //<--- remove this as you are calling it again in the while loop
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
Check this
$connect = mysqli_connect("localhost","turlough","samus1","comments");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
if($rs)
{
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
}
else
{
// no results from query
}
mysqli_close($connect);
}

changing data fetched from server in json format in php

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);
}

Categories