mysql results to a php array

Hi I’m having a bit of trouble with this and wondering what to do. I want to create a php array from mysql results but only some of them out of the 4 will be valid results so for example ADULTMALE and MAST1MALE have values in the database for this particular row but MAST2MALE and SENMALE have no values for this particular query.

Here is my query which I know it works;

[php]$Admalecat = mysqli_query($con,“SELECT ADULTMALE, MAST1MALE, MAST2MALE, SENMALE FROM eventreg WHERE (ID=‘1’)ORDER BY ID DESC LIMIT 1”);[/php]

What I do after is I push them into an array but I only really want the results with the values (This is the bit I am sure I am doing wrong)

I am using a session as I need them later on another page. But anyway, ideally using the example above I would only want the 2 values that exist in the array.

[php]while($row = mysqli_fetch_array($Admalecat)) {
$_SESSION[adultmale] = array();
array_push($_SESSION[adultmale], $row[0]);
array_push($_SESSION[adultmale], $row[1]);
array_push($_SESSION[adultmale], $row[2]);
array_push($_SESSION[adultmale], $row[3]);
}[/php]

Any help muchly appreciated :slight_smile:

You are going about it the wrong way. Modify your query so you only get the results you want.

[php]$Admalecat = mysqli_query($con,“SELECT ADULTMALE, MAST1MALE, MAST2MALE, SENMALE FROM eventreg WHERE (ID=‘1’) AND IS NOT NULL ORDER BY ID DESC LIMIT 1”);[/php]

So if I add “and is not null”

that oughta work?

You could try it, but you might want to specify what you will allow to be null. “is not null” by itself needs a column to work within expected results.

Just thought Id tie up what I did to resolve it;

I created an array with the 4 values then I used array_filter against the mysql query array and kept the results I needed.

That’s taking the long way home. You should ALWAYS have the database do the work when it can. No reason to have the database give you results that you don’t want.

Kevin is right, that database should only be pulling what you want out of it, if the code is doing what the database should, it adds overhead.

Going back to the start:
You have a table that at least has this structure,
ADULTMALE,
MAST1MALE,
MAST2MALE,
SENMALE

You don’t what to show records that have null values in one or more columns. What are you trying to pull from the database and on another area, why does your query have a where clause for (ID = 1) and a limit of 1? Sorting is kind of pointless when you are only asking for a single row.

I did think Kevin was right when he first said, and astonecipher, I have no idea why I have sorted it when it is only going to return ID 1 result. - I can only blame a long day lol.

I may have goofed up with something regarding the not null because it concatenated some spaces into the form that was posting to the db so not null wasn’t actually not null - would also explain why it wasn’t saying null in phpadmin.

I will take a look a with fresh eyes now and not “cheat” lol

Sponsor our Newsletter | Privacy Policy | Terms of Service