This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
i have in my row an array
when
i go this result
{"type":"person1","id":"12"}
I want just to get the id as result value 12
Use json_decode:
$value = ' {"type":"person1","id":"12"}';
$result = json_decode($value, true);
echo $result['id']; // 12
$addedBy = json_decode($row['added_by']);
$id = $addedBy['id'];
Related
This question already has answers here:
JSON encode MySQL results
(16 answers)
Closed 3 years ago.
I am trying to create a key and value array from database record, but the array just getting the final record! Here is a snippet of my code:
$cateogiresArr = array();
while ($row = mysqli_fetch_row($result))
{
$cateogiresArr["categoryname"] = $row[1];
$cateogiresArr["description"] = $row[2];
}
header("Content-type:application/json");
$json_categories = json_encode($cateogiresArr);
On each iteration add new array with required data to $cateogiresArr:
while ($row=mysqli_fetch_row($result))
{
$cateogiresArr[] = [
"cateogryname" => $row[1],
"description" => $row[2],
];
}
This question already has answers here:
mysqli_fetch_array Gives Me Duplicate Rows
(4 answers)
Closed 3 years ago.
enter image description here
$table = $this->Execute("select * from data ");
$result = array();
while($row = mysqli_fetch_array($table))
{
array_push($result, $row);
}
return $result;
this my code,
i dont know why my result including the index
Its because of this statement:
while($row = mysqli_fetch_array($table))
You are getting numeric indexes as well as text keys.
Replace this by:
while($row = mysqli_fetch_assoc($table)) // will return only associate (string) keys.
OR
while($row = mysqli_fetch_array($table, MYSQLI_ASSOC)) // will return only associate (string) keys.
This will not include numeric indexes.
References:
mysqli_fetch_assoc()
mysqli_fetch_array()
This question already has answers here:
Create new variables from array keys in PHP
(5 answers)
Closed 4 years ago.
Is there a way to pass all array's elements to variables with the elements' names?
For example if I have the following array:
$test['name']
$test['phone']
$test['address']
etc..
I want a shortcut for typing this:
$name = $test['name'];
$phone = $test['phone'];
$address = $test['address'];
etc..
Sure you can use $$
$test['name'];
$test['phone'];
$test['address'];
$test['name'] = "John";
$test['phone'] = "987987987";
$test['address'] = "Asheville";
foreach($test as $key=>$val){
$$key = $test[$key];
}
echo $phone;
This question already has answers here:
How to load MySQLi result set into two-dimensional array?
(3 answers)
Closed 6 years ago.
Looking at all the questions are using the depreciated mysql_fetch_assoc/array, hence I don't think that this is a duplicate question.
I have a MySQL table with 5 columns,
ID | NAME | AGE | GENDER | HEIGHT
If I want to store the values of NAME, AGE, GENDER in a PHP array,
$query=$mysqli->query("SELECT NAME,AGE,GENDER FROM TableName")
while($result = $query->fetch_assoc()
{
$array = [];
}
Will my array be stored in the format of
$array=[[Name,Age,Gender],[Name,Age,Gender]]?
If not, what would be my approach in doing this?
It's very simple. You just have to append the result variable in to main array. Like this,
$array =array();
while($result = $query->fetch_assoc()
{
$array[] = $result;
}
$result is an array (fetch_assoc it returns result-set in array) so just append that into main array to get the desired result. ($array=[[Name,Age,Gender],[Name,Age,Gender]])
$data =[];
$i=0;
while($result = $query->fetch_assoc(){
$data[$i][] = $result;
$i++;
}
This question already has answers here:
How to check whether an array is empty using PHP?
(24 answers)
Closed 7 years ago.
I am creating a form to filter the data on my database (Mysql) with queries based on the user's selections.
I am using this code to generate the "data.json" file:
How can I improve the code and check if the array returns empty values? and instead of drawing a chart without bars tells the user to change the selections.
<?php
$rows1 = array();
$rows1['name'] = $varLabel;
while($rr = mysqli_fetch_array($TableData)) {
$rows1['data'][] = $rr[$varLabel];
}
$rows = array();
$rows['name'] = "Registros";
foreach($TableData as $r) {
settype($r['cnt'], "integer");
$rows['data'][] = $r['cnt'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
file_put_contents("data.json", json_encode($result));
?>
Since your array is created by looping over your database result set which you access using mysqli, Use mysqli_num_rows
if(mysqli_num_rows($TableData)==0)
{
// no data
}