-4

I'm developing an android app, which gets data from an mysql-database from server. I'm hosting a local server mysql-database and want to get all data from a table named "require".

In my PHP script I'm doing the following:

<?php
  mysql_connect("localhost", "root", "");
  mysql_select_db("meinstudiumosdatabase");
  $q = mysql_query("SELECT * FROM require");
  echo mysql_error();


  while($row = mysql_fetch_assoc($q)){
      $output[] = $row;
  }

  print(json_encode($output));
  mysql_close();
?>

The mysql_query fails with the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'require' at line 1.

Where is the problem? The require table has two integer columns, nothing special. I have 6 other tables where this script works fine. Hope you can help.


EDIT: Changed title, so people who used "require" as tablename find this easier.

Greetings Kai

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Ka.I.Z
  • 155
  • 1
  • 3
  • 10

2 Answers2

7

require is a reserved keyword in MySQL and you need to enclose it in backticks !

Like this..

$q = mysql_query("SELECT * FROM `require`");

Also, Stop using the deprecated database API [mysql_* functions]. Instead switch to PDO or MySQLi.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

Enclose require with backticks. The reason your query fail is that require is a reserved word.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121