This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am sending a sql query to fetch a data from database but the output is showing the column name as it's value. Following are the codes and output respectively:
<?php
include_once("connect_db.php");
$query1 = "SELECT 'first_name' FROM user_details WHERE email='saptakds#gmail.com'";
$result1 = mysqli_query($conn,$query1);
$myArray = array();
while($row = $result1->fetch_array(MYSQLI_ASSOC)) {
$myArray[] = $row;
}
echo '{"maal":';
echo json_encode($myArray, JSON_UNESCAPED_SLASHES);
echo ',"message" : "success"}';
?>
Output:
{"maal":[{"first_name":"first_name"}],"message" : "success"}
The desired output should have been the following:
{"maal":[{"first_name":"Saptak"}],"message" : "success"}
You should remove the ' on SELECT:
SELECT first_name
FROM user_details WHERE email='saptakds#gmail.com'
Explanation: The column name with ' isn't the value of the column, instead it is the value itself. Maybe you it's a mistake and you want to set the backticks instead of '.
Related
This question already has answers here:
json_encode is returning NULL?
(10 answers)
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
Sorry if this has been asked before but I could not find a solution.
My PHP script returns blank when the data contains an accent and I get no error. How to solve this, please?
My collation is ut8_general_ci.
The server uses PHP 7.
Here is the code:
include ("connectDB.php");
$q = "select id, nom from atelier WHERE session = '2'";
$sql = $mysqli->query($q);
$data = array();
while($row = mysqli_fetch_array($sql, true)){
$data[] = $row;
};
header('Content-Type: application/json');
echo json_encode($data);
Here is the structure of the table:
EDIT: here is the working code
<?php
include ("connectDB.php");
mysqli_set_charset($mysqli,'utf8');
$q = "select id, nom from atelier WHERE session = '3' ORDER BY nom";
$sql = $mysqli->query($q);
$data = array();
while($row = mysqli_fetch_array($sql, true)){
$data[] = $row;
//echo json_last_error(); // returns 5 ?
};
header('Content-Type: application/json');
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>
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 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'];
This question already has answers here:
MySQL query to get column names?
(22 answers)
Closed 9 years ago.
There is a table called as 'myTable' which contains columns - MyFactor1, MyFactor2, and MyFactor3. I want to get those column names, but now it returns only 'MyFactor1'. I tried mysql_fetch_array/assoc/object, but they don't work. Could you have any ideas to resolve this? Thanks in advance. Here is my code:
<?php
$aaa = mysql_query("select column_name from `information_schema`.`columns`
where `table_schema` = 'myDB' and `table_name` in ('myTable')");
foreach ($bbb = mysql_fetch_row($aaa) as $taxokey => $taxovalue) {
?>
<option value="<?php print($taxokey);?>"><?php print($taxovalue);?></option>
<?php
}
?>
mysql_fetch_row() returns an array for each row in the result set. Your initial state for foreach calls mysql_fetch_row() once -- it doesn't call mysql_fetch_row() for each row.
So your foreach is not looping over rows, it's looping over elements of the array returned for one row.
Here's a loop that works the way you intend:
while ($bbb = mysql_fetch_assoc($aaa)) {
$taxokey = $bbb["column_name"];
$taxovalue = $bbb["column_name"];
?>
<option value="<?php print($taxokey);?>"><?php print($taxovalue);?></option>
<?php
}
I'm not sure what different strings you intended $taxokey and $taxovalue to contain. All you get from the query you ran is one string, name of each column.
This question already has answers here:
Truncate a multibyte String to n chars
(4 answers)
Closed 9 years ago.
I am calling all names from a mysql database using php. Some names are very large. so i want to show first 7 charectors and put "..."
Here is my code:
<?php $result = mysql_query("SELECT * FROM Persons WHERE section='one' ORDER BY FirstName");
while($row = mysql_fetch_array($result))
{
$fst = $row['FirstName']; ?>
Any suggestions please?
Try like
if(strlen($row['FirstName']) > 7)
$fst = substr($row['FirstName'],0,7).'....';
try this:
if (strlen($fst) <7) {
echo $fst;
}
else{
echo substr($fst,0,7); echo "...";
}