Jump to content

while loop returns NULL


britus

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.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.