0

I'm a beginner with PHP and I'm stucked with a simple request that returns nothing.

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM order";
$result = $conn->query($sql);

 // output data of each row
while($row = $result->fetch_assoc()) {
     echo "something";
}

$conn->close();

This table is not empty. I'm clueless...

Nico
  • 404
  • 6
  • 17
  • Take a look at [this list at mysql.com](https://dev.mysql.com/doc/refman/5.7/en/keywords.html) for a list of reserved words in SQL. If you're using any of these, it has to be in ticks, take a look at [**When to use single quotes, double quotes, and backticks?**](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Qirel Nov 11 '15 at 00:19
  • Yes the name of my table was reserved. I will take a look at your files. Thank you. – Nico Nov 11 '15 at 00:26

1 Answers1

-1

Your code looks alright.

To get errors that may have occured you can change the following:

$result = $conn->query($sql);

if(!$result) {
    die($conn->error);
}
echo "Result rows: "+ $result->num_rows;

You might find an error this way.

jvecsei
  • 1,936
  • 2
  • 17
  • 24
  • Thank you it says : "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 'order' at line 1". – Nico Nov 11 '15 at 00:16
  • Alright. "order" is a reserved keyword. you might want to try to put that in backticks. \`order\` – jvecsei Nov 11 '15 at 00:17
  • A reserved keyword !!! Damn !!! Thank you, thank you, thank you !!!!!!!!!!!!!!!!!!! It don't work with backticks but that's OK I will rename my table or something. – Nico Nov 11 '15 at 00:21
  • And I was wrong it work with backticks. – Nico Nov 11 '15 at 00:42
  • haha alright. Was also a little bit confused when you said it didn't work. I would appreciate if you could pick my answer as the solution. Thanks :] – jvecsei Nov 11 '15 at 00:44
  • `echo "Result rows: "+ $result->num_rows;` this is wrong, the `+` should be a dot. No idea why this answer was accepted. The real reason was that they used MySQL's reserved word `order` without using ticks around the table name. – Funk Forty Niner May 12 '16 at 19:35