britus Posted November 28, 2018 Share Posted November 28, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 28, 2018 Share Posted November 28, 2018 (edited) 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 November 28, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
britus Posted November 28, 2018 Author Share Posted November 28, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.