0

I have a question regarding applying a custom function to mysql_fetch_assoc while it is in the while loop. See code below.

//current php page
$query = mysql_query("SELECT ... "); //returns multiple rows

while($rs = whileF($query)) {

    //do some fantastic stuff

}

//... functions.php page

function whileF($value = array()) {
    $var = mysql_fetch_assoc($value);

    foreach($var AS $k => $v) {
        $var[$k] = stripslashes($v);
    }

    return $var;
}

My question is I'd like to be able to automatically strip the slashes our of the different fields with this type of a function (and I'd also like to record some debug data if necessary), but it doesn't seem to cooperate too well. If I do a..

print_r($var);

it will return all the values in an array, but by doing the foreach statement I keep getting "Warning: Invalid argument supplied for foreach() in /home/.../includes/functions.php" and if I try this...

$var = array_map('stripslashes',$var);

I get "Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/.../includes/functions.php"

The odd part is, it seems like it applies the foreach/array_map functions properly (the values are being stripped). I don't want to turn off errors, just to make them go away as that would be......less effective programming. I know that the whileF() function works properly cause it's still displaying all the info and if I take away the foreach/array_map statements it'll run without any errors.

Thoughts? I know I'm missing something......

Thanks in advance.

2 Answers 2

2

That's because the last call to mysql_fetch_assoc returns false. You should wrap the foreach in an if:

function whileF($value = array()) {
    $var = mysql_fetch_assoc($value);
    if ($var) {
       foreach($var AS $k => $v) {
            $var[$k] = stripslashes($v);
       }
    }
    return $var;
}
Sign up to request clarification or add additional context in comments.

1 Comment

You have made it on my hero for the day list. For those who care, you can also switch in the array_map() function for the foreach and it'll work like a gem.
1

Although Draevor answered it, there are some questions I'd like to raise.

  1. There is absolutely no point in using stripslashes() on the data you got from the database.
  2. Though there can be a reason to apply another function anyway. But I wouldn't create a custom function for this but rather create a function to get the date right into array,

and then loop over this array to apply whatever modifications required:

$data = $db->getArr("SELECT * FROM table LIMIT 10");
forach ($data as $i => $row) {
  $data[$i]['name'] = htmlspecialchars($row['name']);
}

thus you will have whatever custom function applied without no custom functions.

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.