-1

Please have a look at my code below:

$result = mysql_query("SELECT Value FROM Table");
while(($row =  mysql_fetch_assoc($result))) {

if(strlen($row['Value']) > 1)
{
        //value is not empty or NULL
        echo 'Value found'
}
}

There are 3 values, as expected, being returned by the query - [ ],[ ],[1234567]

The above code works fine. One 'Value found' is echo'd.

However, although the code works as I want it to, it produces the error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource

I know it's just a warning, and my code works fine. But it's annoying getting this warning everytime I run my script.

I have tried using 'array_filter' on the $result variable but as it's a query result it won't work.

### EDIT ###

Nobody seems to understand the situation. This is not a duplicate question. The query is working. Values returned from the query: [], [], [123456]

The warning is coming from the empty array values, which are part of the query results.

mmmbaileys
  • 1,221
  • 4
  • 18
  • 33

1 Answers1

0

You should always check the return value of mysql_query before you use mysql_fetch_assoc When the execution of query fails mysql_query returns false ( a boolean) and when you give this as an input to mysql_fetch_assoc, you get your error.

Print the query just beforemysql_query, run it in mysql and see if it works correctly.

azhar
  • 1,663
  • 1
  • 17
  • 39
  • I'm gather that you're saying to make sure my query is in fact returning data, before using mysql_fetch_assoc. However the problem is that the array of values returned from the query contains both empty values, and normal values. The while loop is checking the contents and throwing the warning when it sees the empty values... – mmmbaileys May 15 '14 at 13:16
  • In summary, the query works, the above code works. But I'm getting a warning for the empty values which are passed to the while loop. – mmmbaileys May 15 '14 at 13:21
  • in my answer you must check your query is executed successfully.if only your query executed successfully you can call mysql_fetch_assoc – azhar May 15 '14 at 13:43
  • I understand and I have included that check. But like I said, the query is working, it is returning values...however some of these values contain no data i.e. [], [], [5432131] – mmmbaileys May 15 '14 at 14:18