1

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));
}



?>
ProgFroz
  • 197
  • 4
  • 17
  • Why don't you just fetch an associative array instead of a numbered array and converting it? If you need to rename columns you could even do that in the select statement. – Chip Dean Mar 30 '17 at 17:25
  • have you checked the error logs? they could provide some useful/vital information. this would be where I would check especially if most of the time you don't get an error. also what is your environment vps, shared hosting, other? – Jpsh Mar 30 '17 at 17:26
  • Your code is vulnerable to SQL injection attacks. You should use [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](http://php.net/manual/en/pdo.prepared-statements.php) prepared statements as described in [this post](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Mar 30 '17 at 17:26
  • Technically it's not alex because he's not putting any outside data into the query. $subid appears to always be defined as 1. Also, he is using mysqli... – Chip Dean Mar 30 '17 at 17:27
  • if `$subId` comes from an outside source in the actual code, that could be the cause of a 500 error. More context is needed for an answer to this question. – shamsup Mar 30 '17 at 17:30
  • There is actually no outside data. What context do you need? – ProgFroz Mar 30 '17 at 17:45
  • @ChipDean Surely $subid is hard-coded here only as an example. My statement wasn't simply, "you should use mysqli," but "you should use mysqli prepared statements." – Alex Howansky Mar 30 '17 at 17:54
  • I will work on this when the code works out well, normally I use prepare statements though. Just trying to make this code work makes me go mad already :D – ProgFroz Mar 30 '17 at 17:57

4 Answers4

1

I think part of the problem is that you're confusing mysql and mysqli functions. You're using mysqli procedurally, i.e.: mysqli_fetch_array(), which doesn't exist. You'll have to convert to mysql, or go with the mysqli functions like this:

$subId = 1;

$con = mysqli_connect('host', 'user', 'pass', 'dbname');

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));
}
Kevin Daniel
  • 431
  • 1
  • 4
  • 13
  • @ProgFroz, post your revised code so that we can see what you did. Also, include the $con variable as you have it, but change the connection info to foo, so that we can see that line in context. It sounds like a syntax error, so the answer is probably in there somewhere. – Kevin Daniel Mar 30 '17 at 18:06
  • Sorry about that, I had a couple of syntax errors myself. Try this: add a semicolon to the end of the debug line and replace the while loop with `while($row = $res->fetch_array())` I did this on my localhost and it worked. – Kevin Daniel Mar 30 '17 at 18:31
  • Come to think of it, you shouldn't even need the while loop. If you rename your fields in your query, you can just `echo json_encode(array("result"=>$res->fetch_array()));` – Kevin Daniel Mar 30 '17 at 18:36
  • Now I get Value – ProgFroz Mar 30 '17 at 19:32
  • When I am at the site I get the right json array though, this worked. Thanks for this. – ProgFroz Mar 30 '17 at 19:33
  • Okay, nevermind. It does not work properly. The site is still blank. – ProgFroz Mar 30 '17 at 19:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139603/discussion-between-progfroz-and-kevin-daniel). – ProgFroz Mar 31 '17 at 14:40
0

change timestamp to timestamp because it's reserved word add backticks

Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
-1

Try using the error reporting in PHP. Enable error reporting by adding the following lines at the start of your code.

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
Krishna Modi
  • 377
  • 2
  • 12
  • this is a bad idea as this is powering an android app, and that information should only ever be displayed in a dev/test environment never in a prod environment – Jpsh Mar 30 '17 at 17:29
  • This can still be used for debugging purpose at staging. Not such a bad idea after all. – Krishna Modi Mar 30 '17 at 18:32
-2

Apparently you forgot

$con = mysqli_connect("localhost","my_user","my_password","my_db");

at the start of your script.

ndufreche
  • 147
  • 6
  • 3rd paragraph `The variable $con does exist, the connection should work properly. I just removed it for stack overflow ^^.` – Dimi Mar 30 '17 at 17:27
  • op said his connection string works fine he just removed it from stack overflow code – Jpsh Mar 30 '17 at 17:28