Hi,

I really have no idea why my if statement is showing no products when the quantity of my products is not 0, if it is then it will show no products, but the quantity of the products is more than 0. Also, there are jsut two items stored in the database. I have connected to the database and I'm sure nothing is wrong with that.

  <?php

      session_start();

      $page = 'index.php'; 


     mysqli_connect("host", "username", "******", "database"); 


     function products () {

       $get = mysqli_query('SELECT ID,Name,Description,Price FROM Products WHERE ID  > 0 ORDER BY ID DESC ');  

       if (mysqli_num_rows($get) == 0) {

           echo 'no products';           
       }
         else {

             echo 'success';

         }
     }



?>

Dani AI

Generated

Most likely cause: the code opens a connection but never keeps the connection handle, then calls the query without that handle. In procedural mysqli the connection must be passed into query calls (or you must use the mysqli object). If the query fails you end up checking a non-result and falling into the "no products" branch. Also the function products() never receives or returns the DB handle, so the query runs in the wrong scope.

Concrete fixes and checklist:

  • Store the connection in a variable and check it for errors.
  • Pass the connection into your query (or use the mysqli object style).
  • Check the query return value before calling row-count or fetch functions.
  • Either pass the connection into products() or return the result from outside the function.
  • As suggested, loop the result set with an associative fetch to print rows.

Example (object style, avoiding the procedural pitfalls shown above):

$db = new mysqli('localhost','dbuser','dbpass','dbname');
if ($db->connect_errno) { die('connect error: '.$db->connect_error); }

$q = "SELECT id, name, price FROM items";
$res = $db->query($q);
if ($res === false) { die('query error: '.$db->error); }

if ($res->num_rows === 0) {
  echo "no products";
} else {
  while ($row = $res->fetch_assoc()) {
    echo htmlspecialchars($row['name']) . ' — ' . number_format($row['price'],2) . "<br>\n";
  }
}
$res->free();
$db->close();

Extra troubleshooting tips: enable full error reporting during development (display_errors / E_ALL), run the SQL directly in phpMyAdmin or the DB console to confirm rows exist, and confirm the table name and DB user privileges. For production, escape output (htmlspecialchars) and use prepared statements for any user-supplied values.

Recommended Answers

All 4 Replies

Member Avatar for Member #949455

I really have no idea why my if statement is showing no products when the quantity of my products is not 0, if it is then it will show no products, but the quantity of the products is more than 0. Also, there are jsut two items stored in the database. I have connected to the database and I'm sure nothing is wrong with that.

Can you provide a little more code?

Where is this:

mysqli_fetch_assoc($get) ?

Oh, I haven't used the mysqli_fetch_assoc. What does this function extactly do?

Where do I excatly put the mysqli_fetch_assoc($get)?

Member Avatar for Member #949455

Oh, I haven't used the mysqli_fetch_assoc. What does this function extactly do?

I read your old threads, this is your first time posting in the PHP section?

So base on your answer you don't know much about PHP.

Are you connected to the database? If not

Read this

http://www.daniweb.com/web-development/php/code/434480/using-phpmysqli-with-error-checking

This function is to get the data from the query.

mysqli_fetch_assoc($get)

since you don't understand it and plus this code is more related to products.

My suggestion is to read up more about this

http://php.net/manual/en/mysqli-result.fetch-assoc.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.