I run a script on my webspace which is used for an android application. Though, it does not seem to work. On some webspaces I get a blank site when I try reaching it, on others I receive a 500 Internal Server Error. Though this script should work properly because it used to do so.
It is supposed to get all information from a table and echo it after some INNER JOINs. I could not track the error even after some mysqli_error checks and some echos to check variables etc.
The variable $con does exist, the connection should work properly. I just removed it for stack overflow ^^.
<?php
$subId = 1;
$sql = "SELECT articles.a_id, sciences.science, articles.title, articles.content, login.username, articles.date, articles.viewed, articles.timestamp FROM articles
INNER JOIN sciences ON articles.s_id = sciences.s_id
INNER JOIN login ON articles.author = login.id
WHERE articles.s_id = ".$subId."
ORDER BY timestamp DESC";
if (!$res = mysqli_query($con,$sql)) {
echo "FAIL";
echo mysqli_error($con);
}
else {
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('a_id'=>$row[0],
'science'=>$row[1],
'title'=>$row[2],
'content'=>$row[3],
'author'=>$row[4],
'date'=>$row[5],
'viewed'=>$row[6],
'timestamp'=>$row[7]
));
}
echo json_encode(array("result"=>$result));
}
mysqli_close($con);
?>
EDIT: I updated my code:
<?php
$subId = 1;
$con = mysqli_connect("HOST", "USER", "PASSWORD", "DATABASE");
if (!$con)
{
echo "Cant't connect to MySQL.<br>";
echo "Debug: " . mysqli_connect_errno()
}
$sql = "SELECT articles.a_id, sciences.science, articles.title, articles.content, login.username, articles.date, articles.viewed, articles.timestamp FROM articles
INNER JOIN sciences ON articles.s_id = sciences.s_id
INNER JOIN login ON articles.author = login.id
WHERE articles.s_id = ".$subId."
ORDER BY timestamp DESC";
if (!$res = $con->query($sql))
{
echo "FAIL";
}
else
{
$result = array();
while($row = $con->fetchArray($res))
{
array_push($result); //use the query to rename fields, if needed
}
echo json_encode(array("result"=>$result));
}
?>