-1

I'm just starting PHP and keep returning null for my SQL query. I am successfully connected to the database and have copied and pasted the query even straight from the database.

<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "customerbasics";


$conn = new mysqli($servername, $username, $password);

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

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

if ($result == NULL){
    
    echo 'No results';
};
if ($result->num_rows > 0) {
  echo 'Rows returned';
  }
} else {
  echo "0 results";
}
$conn->close();
?>

This is what the browswer prints:

Update: Error checking added:

Fatal error: Uncaught mysqli_sql_exception: No database selected

Dharman
  • 30,962
  • 25
  • 85
  • 135
Michelle
  • 7
  • 3
  • It might have `null` data – Zain Farooq Dec 03 '20 at 17:15
  • You should be using a type check; `$var === NULL` – Martin Dec 03 '20 at 17:15
  • 1
    Please enable error reporting and then update the question with the error. [How to get the error message in mysqli](https://stackoverflow.com/a/22662582/4178487)? – Jason K Dec 03 '20 at 17:15
  • you have a typo; remove the semicolon from your IF clause `};` – Martin Dec 03 '20 at 17:16
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Dec 03 '20 at 17:16
  • 1
    You need to specify a database from which you select. You would know that if you enabled mysqli error reporting – Dharman Dec 03 '20 at 17:17
  • You also need to remove the extra `}` you have hanging around.... tidy up your code – Martin Dec 03 '20 at 17:17
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo – Dharman Dec 03 '20 at 17:17
  • @Martin, while that should be corrected, it is actually legal syntax. – Ro Achterberg Dec 03 '20 at 17:28
  • @Martin its not clean but just trying to print some results – Michelle Dec 03 '20 at 17:29
  • @JasonK I have updated with the error! Thanks! – Michelle Dec 03 '20 at 17:30
  • Don't change the question once you received answers. If you have a follow up problem then you can search online for that problem – Dharman Dec 03 '20 at 17:38

2 Answers2

0

Add the fourth parameter to your connection function

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

You have forgotten the database name in your connection. For more information, you can go here. Plus I recommend you to learn PDO or prepared statements

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
-1

First, you have to select database before executing a query:

    mysqli_select_db($conn, $dbname);

Then execute your query:

    $sql = "SELECT * FROM `orders`";

Other way, you can also specify database name directly every executing a query:

    $sql = "SELECT * FROM $dbname.`orders`";
Hida
  • 154
  • 10