2

I have this Code for fetch data from MySql database:

   $sql = 'SELECT id,title,seotitle FROM ' . PROPERTIES_TABLE  . ' WHERE featured = "A" AND approved = 1 AND id = "' . safe($_GET['id']) . '" LIMIT 1';
   $r = $db->query ( $sql ) or error ('Critical Error' , mysql_error());
   $f = $db->fetcharray( $r );
  // Make all values in the array 'n/a' if empty
   $f = array_map ( 'if_empty', $f);  

if_empty is:

function if_empty ( $value ) 

 { 

  if ($value == '' || $value == '0') 
   return ''; 
  else 
   return $value; 

 }

Now, I see this error:

Warning: array_map(): Argument #2 should be an array in C:\xampp\htdocs\cms\qrcode.php on line 26

how can i fix this ?

5
  • The query must not be finding any matching rows, so fetcharray() returns false to indicate that it has reached the end of the results. Commented Apr 17, 2014 at 6:46
  • The problem is that your $db->fetcharray( $r ) doesn't return an array. You can simply ensure by var_dump($f) ing it Commented Apr 17, 2014 at 6:46
  • Is ->fetcharray() your own method? It would be helpful to share that as well. Commented Apr 17, 2014 at 7:03
  • @Jack Yeah sure it is, it's obvious Commented Apr 17, 2014 at 7:04
  • @djay It could be part of a framework; knowing this would help in solving the problem. Commented Apr 17, 2014 at 7:07

2 Answers 2

3

Try:

if ($f) {
    $f = array_map ( 'if_empty', $f);
}
Sign up to request clarification or add additional context in comments.

7 Comments

It would be much better in terms of logical consequence to check if $f is an array rather than 0, false, empty. So if (is_array($f)){} would make a bit more sense here
fetcharray returns an array if it successfully fetches a row, or false to indicate that there are no more rows. This is the logical way to test it.
If you were looping, wouldn't you write while ($row = $db->fetcharray(r))? My if is analogous to this.
@djay I'm with Barmar on this one; $f can only be an array or boolean false. If $f is truthy, it must be an array
I'm assuming it's just a wrapper around mysql_fetch_array, mysqli_fetch_array, or PDO::fetch(PDO::FETCH_ARRAY) and returns whatever they return. If you feel like being pedantic, post your own answer.
|
1

Try:

if(is_array($f))   {
   $f = array_map ( 'if_empty', $f);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.