-1

I need to fetch values from the database. I am using my MySQL mysqli_fetch_assoc with while loop and I don't know the name of the columns.

I am getting null value from the rows.

Here is my code:

$myQuery ='Select * from `tableName` ';  
$query = $conn->query($myQuery);
 while($row = mysqli_fetch_assoc($query))
    {
$data[] = $row;
    }

When I print this $data[] array there are several null rows like this:

{
ID: null,
Car: null,
Phone: null,
State: null
},
{
ID: null,
Car: null,
Phone: null,
State: null
},

How can I avoid these null rows? I don't know the column names.

Alex da Silva
  • 4,552
  • 2
  • 17
  • 25
Jay Zee
  • 263
  • 2
  • 7
  • 18
  • 1
    `select * from tableName where ID is not null` and you keep saying you don't know the column names but you just printed out all four of them twice. – ElefantPhace Oct 07 '15 at 03:13
  • Just filter the null values during query: `SELECT * FROM tableName WHERE Car IS NOT NULL` ? Also, why there are null values in database. You should avoid inserting all-null values – Raptor Oct 07 '15 at 03:14
  • Possible duplicate of [MySQL SELECT only not null values](http://stackoverflow.com/questions/5285448/mysql-select-only-not-null-values) – ElefantPhace Oct 07 '15 at 03:18
  • Further to the *"dont know the name of the columns"* comment, you can use `describe tableName` to get the construction of a table in MySQL. – Rasclatt Oct 07 '15 at 03:25

1 Answers1

0

In MySQL we can use IFNULL() function to find out null values. try this

$myQuery ='Select IFNULL(Car,0),IFNULL(Phone,0),IFNULL(State,0) from `tableName` '; 

              (or)

$myQuery ='Select IFNULL(*,0) from `tableName` '; 
Siddhartha esunuri
  • 1,104
  • 1
  • 17
  • 29