0

EDIT: The problem was fixed. I had to remove all indentation in the file.

I'm trying to return a JSON array from my server but the PHP keeps throwing this error:

[15-Jun-2015 22:46:59 UTC] PHP Parse error: syntax error, unexpected '{' in /home/user/public_html/backend/xstudio/get_all_mods.php on line 10

I've seen many people have problems like this where they're missing a semicolon or something but I just can't find the problem... Here is my code:

<?php

$response = array("success", "message", array()=>"mods");
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/db_connect.php');
$db = new DB_CONNECT();
$result = mysql_query("SELECT *FROM mods") or die(mysql_error());

if(mysql_num_rows($result) > 0) {
    while($row = mysql_fetch_array($result)) {
        $mod = array();
        $mod["mod_id"] = $row["mod_id"];
        $mod["name"] = $row["name"];
        $mod["author"] = $row["author"];
        $mod["description"] = $row["description"];
        $mod["download"] = $row["download"];
        $mod["picture"] = $row["picture"];
        $mod["created_at"] = $row["created_at"];
        $mod["updated_at"] = $row["updated_at"];
        array_push($response["mods"], $mod);
    }
    $response["success"] = 1;
    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No mods found";
    echo json_encode($response);
}
?>

Any and all help is appreciated, thanks!

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390

1 Answers1

0

Try while($row == mysql_fetch_array($result))?

= is assignment. == is equal-to.

Mark A. Ropper
  • 375
  • 3
  • 12
  • Nope, I tried that right after I asked, same result. – Unknown User Jun 15 '15 at 23:07
  • Sorry for the stock response. According to [link](http://php.net/manual/en/function.mysql-fetch-array.php) this link mysql_fetch_array() is deprecated. What version of php are you using? – Mark A. Ropper Jun 15 '15 at 23:14
  • I am using PHP Version 5.5 Though I switched to 5.4 to test it and it still threw the same error. – Unknown User Jun 15 '15 at 23:17
  • If I try to run it on my machine, with most of the code gutted (I have no idea what's in your db, or how you connect to the database), I get no errors. Might I suggest taking a look at `db_connect.php` to see if there is something wrong there? Also I get a warning of `Illegal offset type` on line 3. However, word of warning, if I remember correctly using these `mysql_` functions can leave you open to security problems like code injection, might be worth taking a look at [PDOs](http://php.net/manual/en/ref.pdo-mysql.php) – Mark A. Ropper Jun 15 '15 at 23:35
  • Restructuring $response removes the illegal offset type, `$response = array("success"=>0, "message"=>"", "mods"=>array());`. Don't think I can help any more with this (at least tonight), sorry. Good luck. – Mark A. Ropper Jun 15 '15 at 23:47
  • Ok, thank you for your help so far, I'm getting closer to figuring it out thanks to you. – Unknown User Jun 15 '15 at 23:51
  • I figured out the problem. For some reason, I had to remove ALL indentation in my PHP file; the parser couldn't handle it I guess. I removed that and now the PHP returns the correct JSON. Thank you for the tips! – Unknown User Jun 16 '15 at 01:06