i'm trying to get data from MySql Database. This is my code.
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Quotes WHERE Id=2";
if (mysqli_connect_errno()) die('Could not connect: ' . mysql_error());
$return_arr = array();
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$row_array['Id'] = $row['Id'];
$row_array['Quote']= $row['Quote'];
$row_array['Author'] = $row['Author'];
$row_array['Category'] = $row['Category'];
$row_array['Likes'] = $row['Likes'];
array_push($return_arr, $row_array);
}
}
echo json_encode($return_arr, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
mysqli_close($conn);
On the web page nothing appears to me, I found the problem is due to the accented characters like "è or "ò". In fact when i try this:
<?php
echo "Result 1";
$return_arr = array();
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$row_array['Id'] = $row['Id'];
$row_array['Quote']= $row['Quote'];
$row_array['Author'] = $row['Author'];
$row_array['Category'] = $row['Category'];
$row_array['Likes'] = $row['Likes'];
array_push($return_arr, $row_array);
}
}
echo json_encode($return_arr, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo " Result 2 ";
$sql2 = "SELECT * FROM Quotes WHERE Id=2";
if (mysqli_connect_errno()) die('Could not connect: ' . mysql_error());
$return_arr2 = array();
if ($result2 = mysqli_query($conn, $sql2)) {
while ($row = mysqli_fetch_assoc($result2)) {
$row_array['Id'] = $row['Id'];
$row_array['Quote']= $row['Quote'];
$row_array['Author'] = $row['Author'];
$row_array['Category'] = $row['Category'];
$row_array['Likes'] = $row['Likes'];
array_push($return_arr2, $row_array);
}
}
echo json_encode($return_arr2, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
mysqli_close($conn);
?>
I get this result on the screen: Result 1[ { "Id": "1", "Quote": "My Simple Text", "Author": "Me", "Category": "X", "Likes": "0" } ] Result 2
I'm using php 7.2 and the latest version or MySql. This is how I configured my database Database structure Database simple Data for testing
UPDATE: I changed all the encodings in utf8_bin but it had no effect. Database encoding Columns encoding Thanks for your attention.