This question already has answers here:
JSON - slashes not escaping
(2 answers)
Closed 9 years ago.
This my PHP script:
<?php
header('Content-Type: application/json; charset=utf-8');
$link = mysql_pconnect("localhost", "test", "test") or die("Could not connect");
mysql_select_db("myradio") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM radio1");
while($obj = mysql_fetch_assoc($rs)) {
$arr[] = $obj;
}
echo '{"success":true,"error":"","data":{"schedule":['.json_encode ($arr).']}}';
?>
The JSON displays well, however, slashes are not escaped, which results in:
It's the weekend
when it should be:
It\'s the weekend
within the JSON.
Also how can I manipulate my PHP/JSON so that depending on callback, it gives this error message:
({"success":false,"error":"File does not exist"});
It's working well so far, just need to get the finer details right, would appreciate some help!
As for my PHP, I am using PHP 5.4.10 on a MAMP server, if that's of any relevance.
Read this article with attention. It describes how you can escape special symbols like '\' by encoding them with different options like JSON_UNESCAPED_UNICODE in the json_encode function.
Related
This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 5 years ago.
I am continuously having this internal server error to a very simple php code working with JSON. The values I am logging from the code is absolutely correct, yet when in action I am getting an internal server error 500 from this particular code. I had a similar code working previously. What am I doing wrong? or how should I proceed with debugging the error?
<?php
$var2 = $_POST['phn'];
$phone_received = json_decode($var2);
$adb = PearDatabase::getInstance();
$query1 = "SELECT addressid FROM address WHERE mobile = ?";
$leadID = $adb->pquery($query1, array($phone_received));
$row= $adb->num_rows($leadID);
if ($row != 0) {
$result = 'This number has already been used in the system.';
echo json_encode($result);
}else{
$result = 'Good to go!';
echo json_encode($result);
}
?>
Did you try to add true parameter to your json_decode call? I suppose the problem is with the $phone_received param. I also suggest you line by line debugging: try to retrun some dummy result after each line of code step by step so you can detect the place where the error occurs.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
Hey so i'm getting text with latin characters like "ó" from a mysql database. This is the connection along with the query i'm using to get the data, placing it in an array:
<?php
header('Content-Type: text/html; charset=UTF-8');
$con=new mysqli ("localhost","root","","chroniclemark");
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
while ($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
$lol5[] = $row['descricao'] ;
}?>
I'm then trying to display the the text outside of the <?php ?> statement like so:
<p ><?php echo $lol5[0]; ?></p>
All of the text is fine except for the "ç", "ó", "ã" and so on.
I also have a meta tag <meta charset="UTF-8"> in my header but it doen't help.
In the database, the data is set to "utf8mb4_unicode_ci".
What can i do to fix this? Any idea?
#Fred -ii Thanks for pointing me in the right direction in the comments.
All i had to do was use mysqli_set_charset($con, 'utf8mb4'); before making the query
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
It's all fine now :)
Hey I am iOS developer I am trying to create simple JSON output from my website. I found good start link and here is some explanation how to do it.
So I've created accounts.php file and put it to my public_html folder
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
Of course I use my user and password and I pointed my just created database ob my end.
so when I try to request my file so http//mywebsite.com/accounts.php there is no data.
I tried to use google chrome and Postman so it says No response received when I switch to JSON. For HTML there is no info in Postman.
My question how can I test it? even if I use Echo(123) before include_once("JSON.php"); line there is no 123 on html page.
I tried to test PHP with only this code:
<?php
phpinfo();
?>
and it works. I have PHP Version 5.4.32
First of all, simply use PHP's function json_encode($arr). It does exactly what you are asking for and is pretty much included in every version of PHP that I can think of.
Documentation
Also, I am not sure if this is the issue, but you may want to change Echo ==> echo. This is generally convention at the very least.
SUPER IMPORTANT
Finally, DO NOT USE mysql extension. Its is dangerous, may not work correctly, and has security vulnerabilities. Use mysqli or PDO.
Matrosov -
You are very close. Use the json_encode function to output your code via the PHP manual. Also consider using mysqli instead of mysql for your database connection as it has been better support for modern MySQL servers.
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/book.mysqli.php
<?php
include_once("JSON.php");
$link = mysqli_connect("localhost", "user", "pass") or die("Could not connect");
$link->mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo json_encode($arr);
?>
This question already has answers here:
UTF-8 all the way through
(13 answers)
Non English characters appear as question marks on my php page - appear fine in database
(2 answers)
Closed 7 years ago.
I have saved my data on MYSQL database using "utf8_general_ci". I wrote it on Bangla font.
But when I am trying to show data using php it is showing "????..." instead of showing the charecters.
<?php
$con = mysqli_connect("localhost","appsaadr_edu","pass","appsaadr_edu");
$result = mysqli_query($con,"SELECT * FROM lnews ORDER BY id DESC");
echo "{\"news\":";
$arr = array();
while($rows = mysqli_fetch_array($result)){
$test['id'] = $rows['id'];
$test['title'] = $rows['title'];
$test['text'] = $rows['text'];
$test['time'] = $rows['time'];
array_push($arr,$test);
}
echo json_encode($arr);
echo "}";
?>
What to do now?
Make sure the file is encoded in UTF-8 without BOM
Add : mysqli_set_charset($con,"utf8"); between the lines 3 and 5
Put all the $test['XXX'] like this utf8_encode($test['XXX'])
Make sure to add the data into the database also through utf8_encode before
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 8 years ago.
The error is
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\posdef\index.php on line 18
Evidently, I perceive that the query returns an object resource, but how to convert it into a string. I am caught up in that problem. How to convert the object resource into string and store it in a variable?
This is the piece of code where I am trying to recover the data stored -
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "new");
$mylogo = mysqli_query($con, "SELECT first FROM hello WHERE sno=6");
You got the data, now you just need to fetch it:
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "new");
$mylogo = mysqli_query($con, "SELECT first FROM hello WHERE sno=6");
while ($row = $mylogo->fetch_assoc()) {
echo $row["first"];
}
Check out the docs, there are additional examples. mysqli_fetch_assoc
Once you run the query, you have to read the results. If you're only expecting ONE row back, you can do the following instead of a while loop:
$rslt = mysqli_query($con, "SELECT first FROM hello WHERE son=6");
$row = $result->fetch_assoc();
$mylogo = $row['first'];