Context
I just upgraded my website server to a VPS and, since moving my files to the new server, have been having some problems with the transition. One of them is that, on this server, one of my functions isn't running correctly. The function getRandomArticles()
gets user-posted articles from my mySQL database. On my local server and on my previous shared server, it worked fine. However, when I run the following function on the VPS, I get the errors parserror
and
SyntaxError: Unexpected end of JSON input at parse (<anonymous>) at VM3130
jquery.min.js:2 at l (VM3130 jquery.min.js:2) at XMLHttpRequest.<anonymous>
(VM3130 jquery.min.js:2)
(from console.log(error)
and console.log(errorThrown)
in the error callback function)
Here's the function getRandomArticles()
that returns the errors:
var ids = [];
function getRandomArticles() {
$.ajax({
url: 'includes/articles/getRandomArticles.php',
type: 'GET',
dataType: 'json',
success: function(data) {
//code irrelevant to problem
},
error: function(requestObject, error, errorThrown) {
console.log(error);
console.log(errorThrown);
}
});
}
Here's my PHP code:
<?php
include '../dbh.inc.php';
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$q = "SELECT articles.article_id, articles.title, articles.article, articles.idUsers, users.uidUsers, articlecoverimages.image, profileimages.image AS profileImage
FROM articles
JOIN users
JOIN articlecoverimages
JOIN profileimages ON users.idUsers = articlecoverimages.idUsers AND articles.article_id = articlecoverimages.article_id AND profileimages.idUsers = users.idUsers
WHERE articles.published = ? AND articles.approved = 1
ORDER BY RAND()
LIMIT 5";
$stmt = mysqli_prepare($conn, $q);
mysqli_stmt_bind_param($stmt, 's', $published);
$published = "yes";
mysqli_stmt_execute($stmt);
$r = mysqli_stmt_get_result($stmt);
if ($r) {
if (mysqli_num_rows($r) == 0) {
$data['error'] = "";
$results[] = $data;
} else {
while ($row = mysqli_fetch_array($r)) {
$data['articleId'] = $row['article_id'];
$data['title'] = $row['title'];
$data['article'] = strip_tags(htmlspecialchars_decode($row['article'], ENT_QUOTES));
$data['idUsers'] = $row['idUsers'];
$data['uidUsers'] = $row['uidUsers'];
$data['image'] = $row['image'];
$data['profileImage'] = $row['profileImage'];
$results[] = $data;
}
}
echo json_encode($results);
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
There is no response in the PHP code when these errors occur. Without the error, it responds with the JSON array. However, when I echo a specific row, such as echo $row['article'];
, that specific piece of data sends, even with the error. So, nothing but the names of the articles would be returned in that scenario.
When I refresh the page many times, the code finally works and the article data loads. However, this only works about 30-40% of the time, seemingly randomly.
Things I Have Tried
I use echo json_encode($results)
to return the values of the PHP file that the AJAX call points to, so I'm using JSON here. When I just say echo $results[]
, there is an error, and when I remove dataType: json
from the AJAX parameters, there is also an error (in case those had any legitimate potential for solving the problem).
Question
Any idea what could be going wrong with this function?
Update
I find that when I remove the line
$data['article'] = strip_tags(htmlspecialchars_decode($row['article'], ENT_QUOTES));
All the other rows return in the PHP file. And when I remove every other line but the line above, the error continues. Every time. So, removing
$data['article'] = strip_tags(htmlspecialchars_decode($row['article'], ENT_QUOTES));
fixes the issue EXCEPT for the fact that I need to be able to return this line, which gives the article content itself. I have this same problem with three other files that get article content, all of which have the same line (and all of which are fixed by removing that line). It's weird, though, because other files that specify an article topic are able to get the article content just fine. I've tried resetting everything on my VPS; the database, the apache server, everything I could, to no avail. Based on this finding, it seems that the PHP file is the problem here, but no potential solution seems to be fixing the problem. Again, the file works just fine in my local server and worked just fine on my previous shared server, but on this new server, it's having problems. I even deleted the files and made new ones to see if the file itself caused any sort of error, which also didn't work.
Additionally, as previously stated, although
$data['article'] = strip_tags(htmlspecialchars_decode($row['article'], ENT_QUOTES))
is causing problems, when I remove echo json_encode($results)
and all the other column values stored in the $results
array and simply echo $row['article']
(like so):
while ($row = mysqli_fetch_array($r)) {
echo strip_tags(htmlspecialchars_decode($row['article'], ENT_QUOTES));
}
the article content returns. Any idea why this problem may be occurring?
Here's the structure of the 'article' column in the articles database:
article TEXT NOT NULL