I have a table full of strings in a database, about a sentence long each. Some have quotation marks in them or apostrophes. I run a fetch in React-native, which runs a php file. This php file gets a random item from the table and echos it so the javascript can parse the JSON string and do things with it. If the string has quotation marks or apostrophe(s), the app crashes with "JSON parse error: unexpected EOF". I have tried addslashes() and I have even tried using the php function str_replace() to replace quotations and apostrophes with obscure characters. Either way, I still get the crash and the same error message. Here is part of the code here:
$get_fate = "SELECT * FROM $fates ORDER BY RAND() LIMIT 1";
$run_fate = mysqli_query($con, $get_fate);
$num_fates = mysqli_num_rows($run_fate);
if ($num_fates == 1) {
$row_fate = mysqli_fetch_assoc($run_fate);
$fate = $row_fate['fate'];
$msg = addslashes($fate);
$msg_json = json_encode($msg);
echo $msg_json;
} else ...
And the javascript side:
getFate = async () => {
let user = await AsyncStorage.getItem('email');
fetch('fetch address goes here', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application.json',
},
body: JSON.stringify({
email: user
})
}).then((response) => response.json())
.then((responseJson) => {
this.setState({fate: responseJson});
}).catch((error) => {
console.error(error);
});
}
Here is an image of the crash:
And if it means anything, I was sent a big word file of all these "fates". I used fates.splitlines() in python to separate them all into separate strings and put them into an array, which I looped through in php and inserted into the database. I then noticed there was some kind of klingon looking text where quotes and apostrophes should be, so I "fixed" that with the following SQL queries:
UPDATE choose_your_fate SET fate = REPLACE(fate, '“', '“');
UPDATE choose_your_fate SET fate = REPLACE(fate, 'â€', '”');
UPDATE choose_your_fate SET fate = REPLACE(fate, '’', '’');
UPDATE choose_your_fate SET fate = REPLACE(fate, '‘', '‘');
</div>