0

UPDATE: I've edited the question to be more specific:

so the problem was a hidden character i could only see openning the file on my webhost editor, and then it was missing '' on the $id

So, i've already looked everywhere for an answer, and i simply can't find it, so was wondering if someone can point me in the right direction.

I am following this tutorial: https://www.simplifiedcoding.net/android-mysql-tutorial-to-perform-basic-crud-operation/ to learn how to make CRUD operations connecting my app with MySQL server, and i got stuck on the part to get a single entry from table.

It gives me a "Parse error: syntax error, unexpected 'echo' (T_ECHO) in /home/u728774671/public_html/db/getpost.php on line 28" and i can't figure out why, my code is exactly as the one in the tutorial, except i changed name of table and variables.

what am i missing? am i doing something wrong or is it the tutorial code?

here is the code:

<?php 
//Getting the requested id
$id = $_GET['id'];

//Importing database
require_once('dbconnect.php');

//Creating sql query with where clause to get an specific employee
$sql = "SELECT * FROM posts WHERE id='$id'";

//getting result 
$r = mysqli_query($con,$sql);

//pushing result to an array 
$result = array();
$row = mysqli_fetch_array($r);
array_push($result,array(
"id"=>$row['id'],
"titulo"=>$row['titulo'],
"usuario"=>$row['usuario'],
"endereco"=>$row['endereco'],
"post"=>$row['post'],
"imagem"=>$row['imagem']
 ));

//displaying in json format 
echo json_encode(array('result'=>$result));

mysqli_close($con);
?>

this is the line where the error is:

echo json_encode(array('result'=>$result));

and this is the code from the tutorial:

 <?php 

 //Getting the requested id
 $id = $_GET['id'];

 //Importing database
 require_once('dbConnect.php');

 //Creating sql query with where clause to get an specific employee
 $sql = "SELECT * FROM employee WHERE id=$id";

 //getting result 
 $r = mysqli_query($con,$sql);

 //pushing result to an array 
 $result = array();
 $row = mysqli_fetch_array($r);
 array_push($result,array(
 "id"=>$row['id'],
 "name"=>$row['name'],
 "desg"=>$row['designation'],
 "salary"=>$row['salary']
 ));
 
 //displaying in json format 
 echo json_encode(array('result'=>$result));

 mysqli_close($con);
 ?>
cchamberlain
  • 17,444
  • 7
  • 59
  • 72

1 Answers1

0

I am not currently on my pc, so I cant test your code, but i think you are getting a wrong json or something, could you print_r the json_encode

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • oh my god, you won't believe it, there was a hidden random character that i could only see by openning the file on my host editor"now i'm getting a new error: **Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/u728774671/public_html/db/getpost.php on line 17**, to which i'm already searching for an answer – Nessa Stagg Dec 19 '16 at 00:45
  • That error is saying that the resulting `mysqli_query` has an error, try to parse that mysqli and see if it has any error on sql query, look here http://php.net/manual/en/mysqli-result.fetch-array.php – matiaslauriti Dec 19 '16 at 00:49
  • yep, i got it already, now i'm having problems with JSON, it's returning html code, which is giving an excpetion, i tried substring to look for the issue but it just goes on from to to an entire html page, guess i'll just close this one and make another question for the JSON Exception, i'm searching for answers but can't really find why – Nessa Stagg Dec 19 '16 at 01:35
  • Yes, create a new one – matiaslauriti Dec 19 '16 at 04:24