0

I am using the following PHP code to retrieve the "Location" field from the database in my server, but am getting the error shown below when I run the PHP.

My PHP file:

<?php 
$con=mysqli_connect("localhost","naveen","naveen123","naveen_z");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT 'Location' FROM `map_details` ");
print_r($result);
mysqli_close($con);
?>

My database structure:

Database structure

I am getting the following error:

Parse error: syntax error, unexpected $end in location.php on line 12

Now, I'm receiving this:

{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":n‌​ull} – 
4
  • 1
    Observation: Don't mix and match identifier quotes. Use ` consistently (or, better, only quote when required). ' should not be used for identifiers because it's actually an ANSI SQL string quote :( Commented Nov 16, 2013 at 7:03
  • can u make that php in correct format becoz am getting following null values now it showing all values as null {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":n‌​ull} – Commented Nov 16, 2013 at 7:10
  • $result refers to the entire result, not a specific row. It says "there were no rows" ("num_rows":null) so the map_details table is most likely empty. Anyway, that's a different (and unrelated) problem - post a new question as appropriate (but first validate or disprove my previous assertion). Commented Nov 16, 2013 at 7:27
  • no check this image of that table tinypic.com/r/2vxjfps/5 Commented Nov 16, 2013 at 7:37

3 Answers 3

1

You are using quotes instead of backticks,so you are selecting a string.If its not a reserved word you can remove them altogether.

mysqli_query($con,"SELECT Location FROM `map_details`")
Sign up to request clarification or add additional context in comments.

Comments

1

Make sure to use msqli_fetch_array() on the results of the query.

EG: return mysqli_fetch_array($result);

Comments

0

You are missing closing quotes on your query:

mysqli_query($con,"SELECT 'Location' FROM `map_details`");

what's funny is that stackoverflows syntax highlighting caught it!

Also, unless you are trying to select the literal value 'Location' from your table, you want different quotes (either backticks or remove them altogether)

3 Comments

oooops..my mistake,...now its working but the $result printing like "mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 9 [type] => 0 ) "how do i get the reults in JSON
well, your result is an object, you can call json_encode on the object, and that will convert all public variables into json format.
it showing all values as null {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.