Jump to content
britus

while loop returns NULL

Recommended Posts

Hi, I am trying to expand my very limited PHP by building a CMS. It's all new to me so please be courteous with your replies 😊

I little history of my problem.

I am running xampp on a windows 10 running php 5.6.38

I have taken two video courses and on each I got stumped halfway through with a very similar problem.
The first was using mysql which I was converting to mysqli until I run into a problem.
I then started a new course which used mysqli but it was from 2014 so not sure which version of PHP or if it would even matter.

I am copying everything word for word and even downloaded the course file on the first one and still got the same problem so I'm wondering if there is something wrong with my setup - config?.

The problem.
I am trying to pull data from the database using a while loop. Each time it returns NULL.

This is the code:
 

<?php 

error_reporting(E_ALL);
ini_set('display_errors',1);

include("includes/database.php");
include("includes/header.php");

if ($connection) {        
    echo "Great, we're connected to the database<br><br>";
    }else {
        echo "Bummer, it didn't connect to the database!";
        }

     $query = "SELECT * FROM categories";
   $result_set = mysqli_query($connection, $query) ;

   if (!$result_set) {
           die("Query Failed".msqli_error($connection));
               }

   while ($row = mysqli_fetch_array($result_set)) {
               echo $row['cat_title'];
               }

    var_dump($result_set);                      
        
 ?>


This is the response:

Great, we're connected to the database

object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }

Question:
the database table (categories) is populated with two rows (id & cat_title) and there is data in there.
So why does it not retrieve the data?

 

Thanks for your help.

Share this post


Link to post
Share on other sites

Are you sure you are connecting to the database that has 2 records in the categories table? That one does appear to be empty.

[EDIT} PS - I would definitely recommend switching to PDO instead of mysqli.

Edited by Barand

Share this post


Link to post
Share on other sites

Hi Barand,

Now I feel like an idiot. I had (now deleted) a couple of very similarly named db,s. I was connected to the wrong one.

PDO, yes definitely I want to do that but as this course is using mysqli, I want to get the thing running first then I will use that to figure out PDO. Trying to not over complicate myself.

Thanks for putting me right.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.