Iterate through result set

OK, I’m sure I’m being blind again and missing something obvious, but i can’t figure out what.

I am running a query against my DB and it is returning 0 results, even though when I run the query directly against the database using my DB admin panel it returns exactly what I expect it to.

Here is my code:

$GetCategories = “SELECT ID, Category FROM Work_Categories WHERE Active = ‘1’”;
$GetCatResult = mysqli_query($db_server, $GetCategories)
or die("Unable to run query: " . mysqli_error($db_server));
$NumResults = mysqli_num_rows($GetCatResult);
echo $NumResults;
while($Category = mysqli_fetch_array($GetCatResult))
if ($Category[‘ID’] == $EmpDetails[‘workcatid’])
echo “<option value='” . $Category[‘ID’] . “’ selected=\“selected\”>” . $Category[‘Category’] . “</option>” . "
} else {
echo “<option value='” . $Category[‘ID’] . “'>” . $Category[‘Category’] . “</option>” . "

The $NumResults and subsequent echo statement I just put in trying to see what I am returning and it shows me 0. There should be about 10 records returned that I want to use to populate a dropdown field on my website.

Hopefully someone can give me that gently nudge (or a hard smack) to get me back on the right track. :smiley:


Try this line to see if your query runs successfully,

$GetCatResult = mysqli_query($GetCategories) or die("Unable to run query: " . mysqli_error($GetCategories));

Nope, got these errors when I changed those values:

Warning: mysqli_query() expects at least 2 parameters, 1 given in /var/www/vhosts/ on line 4

Warning: mysqli_error() expects parameter 1 to be mysqli, string given in /var/www/vhosts/ on line 5
Unable to run query:

Then the old should be fine,

$GetCatResult = mysqli_query($db_server, $GetCategories) or die("Unable to run query: " . mysqli_error($db_server));

Are you sure that there are records which are active?

Yep, I take that EXACT query (cut/paste) and run it in HeidiSQL linked to the same database… CRAP! HeidiSQL is linking to my LIVE database, NOT me dev one. See? I knew it was me being stupid!

Sheesh! Maybe it’s time to call it a night. No results happens when you have failed to populate your development database with real data! Sheer BRILLIANCE on my part!
Thanks again for the gentle slap upside the head, I promise I’ll quit for the night.

You can include this;

$mysqli =mysqli_connect($server,$username,$password,$database);

you are not create an instance of the connection to the database…

And then execute query like this:::

$result=mysqli_query($mysqli,“your query”);

Included file,