-1

I have written a php script for returning every thing in database in json format. The following code is giving me this 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 ''userCredentials'' at line 1

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

here is the code:

<?php

require 'testconnect.php';

$sql = "SELECT * FROM 'userCredentials'";

 $query = mysql_query($sql);

 if($query == false){
  echo mysql_error();
echo 'failed query connect';

}
echo 'new success';

while($row = mysql_fetch_assoc($query)) {

  echo json_encode($row);

}

?>

What is wrong with this code? Am I missing something?

PS: I am not PHP programmer. I wrote this script to use this in my java code.

Regards

Umer Farooq
  • 7,246
  • 7
  • 41
  • 60

3 Answers3

2

This is possibly caused from the fact your using quotes to symbolize a string, you should be using back ticks to identify column names, table names

$sql = "SELECT * FROM 'userCredentials'";

Should be;

$sql = "SELECT * FROM `userCredentials`";
Daryl Gill
  • 5,398
  • 8
  • 32
  • 69
2

Your real error message is 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 ''userCredentials'' at line 1

You need to fix your SQL query to read SELECT * FROM userCredentials no single quotes around the table needed.

As other have stated, backticks is the correct way enclose the table name, or any string that could be interpreted incorrectly by mySQL.

Steven V
  • 15,889
  • 3
  • 60
  • 74
  • +1 for the last paragraph. It can be done without back ticks but that may lead to another issue if user is not careful about the naming :) – Fallen Jul 20 '13 at 18:54
0

Remove the single quotes arround the table name instead you can use back ticks

$sql = "SELECT * FROM `userCredentials`";
M Khalid Junaid
  • 62,293
  • 9
  • 87
  • 115