0

I have the following code

<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, name, time, order FROM main";
$result = mysqli_query($conn, $sql);

$num = mysqli_num_rows($result);
echo "Stuff: " . $num;

mysqli_close($conn);
?>

For some reason, this does not return a value when uploaded to my server and run on my web browser. Can anyone understand why this might be?

Many thanks

John Smith
  • 359
  • 3
  • 13
  • 2
    Because your query failed. `order` and `time` are [reserved words](https://dev.mysql.com/doc/refman/5.5/en/keywords.html) – Phil Jun 17 '16 at 00:03
  • 2
    Put this at the top of your script `ini_set('display_errors', 'On'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` – Phil Jun 17 '16 at 00:04
  • 1
    You should wrap your columns with backticks, as Phil said, they are reserved words. `SELECT \`id\`,\`name\`,\`time\`,\`order\` FROM main` – Dave Chen Jun 17 '16 at 00:04
  • 1
    @Phil `order` is a reserved word, `time` is a keyword but not reserved. – Barmar Jun 17 '16 at 01:12
  • @Barmar thanks for the clarification – Phil Jun 17 '16 at 01:19
  • Even faster: var_dump($conn) you can see the database is not set. You know you just need to re-establish db connection for whatever reason. My similar symptoms were after adding .env and an AJAX call, forgot to put it on the routes controller. – AdheneManx Oct 20 '18 at 05:08

0 Answers0