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);
?>
Related
I need some help please.
Currently my mariadb is set up with one column as a json array.
I would like to search the array from a users input, then combine it with the first and last name of the user stored in the database.
In the database:
Columns: firstname, lastname and suburbs_i_cover (containing the json array)
ex: ["Pretoria","Cape Town","Garden Route"]
I would like the sql statement that would search the suburbs_i_cover and allow me to combine the first and last name.
So far I have this:
<?php
//echo JUri::getInstance();
if (isset($_GET['category'])) {
$catetogry = $_GET['category'];
$name = $_GET['name'];
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query = ("SELECT * FROM #__jsn_users"); //not sure what goes here
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $obj) {
$value = json_decode($obj->suburbs_i_cover);
$value = array_filter($value, 'strlen'); //removes null values
but leaves "0"
$value = array_filter($value); //removes all null
values
if (!empty($value)) {
$value2 = $obj->firstname;
$value3 = $obj->lastname;
echo $value2 . ' '. $value3;
//echo "<br>";
// echo sizeof($value);
echo "<br>";
if (is_array($value)) {
foreach ($value as $sub_value) {
echo $sub_value;
echo "<br>";
}
}
echo "<br>";
echo "<hr>";
}
}
}
I got a working solution:
To use: in_array($name, $value)
<?php
//echo JUri::getInstance();
if (isset($_GET['category'])) {
$catetogry = $_GET['category'];
$name = $_GET['name'];
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query = ("SELECT * FROM #__jsn_users");
$db->setQuery($query);
$results = $db->loadObjectList();
foreach($results as $obj) {
$value = json_decode($obj->suburbs_i_cover);
$value = array_filter($value, 'strlen'); //removes null values
but leaves "0"
$value = array_filter($value); //removes all null
values
if (in_array($name, $value)) {
$value2 = $obj->firstname;
$value3 = $obj->lastname;
echo $value2 . ' '. $value3;
echo "<br>";
echo "<hr>";
}
}
}
?>
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);
}
}
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"
}
I can't save data to database from file_get_html($url) function. My script is showing data nicely by scraping from a url. But I can't save the showed data to a database. It shows error between object and array. I can't even show data by array index. Such as $value[0]. Here is my code sample:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
}
mysqli_close($con);
?>
Try this and let me know if it works:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
}
$value = base64_encode($value);
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
mysqli_close($con);
?>
$value need convert to a string before write to DB. You can show array or object use
echo '<pre>';
print_r($value);
echo '</pre>';
or
var_dump($value)
table data(id, name)
function getData() {
$data = array();
$sql = 'Select * From data';
$query = mysql_query($sql);
if(!$query) {
echo "Error: " . mysql_error();
exit;
}
while($row = mysql_fetch_array($query)) {
$data[] = $row;
}
return $data;
}
$data = $this->getData();
foreach($data as $dt) {
echo $dt->name;
}
I get an error when I echo $dt->name;, the output is null, How do I fix it ?
$dt is not an object but a array. $dt->name should be $dt['name'].
Try:
var_dump($data);
//if its not a class then simply do
$data = getData();