Q:

PHP: I can only use this function once (using it in a while loop)

I got an answer on an older question which is almost working.
I have a function,
function vraagOp($table,$where)
{
    static $rVraagOp;
    if(!$rVraagOp){
        $qVraagOp = "SELECT * FROM $table WHERE $where";
        $rVraagOp = mysql_query( $qVraagOp );
    }
    return mysql_fetch_assoc( $rVraagOp );
}

that I want to use like this
while (vraagOp("testtable","testtype = test")) 
{
   echo "testing <br>";
}

The function works fine, however, I can only use it one time per page. The second time I call it it doesn't do anything. No error either, it's just like the function never happend.
What do I have to change in order to make it work multiple times and still work in the while loop?

A:

The error is because you're not resetting the mysql result. Because it is being stored in a static variable, the function is trying to access the same result resource every time. I can see that you're trying to cut out a step from your queries (combining the querying and retrieving steps into one), but I wouldn't bother with it if I were you: the benefits do not outweigh the costs in loss of flexibility. Stick to the tried and true way:
$result = mysql_query("SELECT * FROM foo");
while ($row = mysql_fetch_assoc($result)) { ... }

// loop through it again:
mysql_data_seek($result, 0);  // rewinds the result
while ($row = mysql_fetch_assoc($result)) { ... }

Or even better, take a look at the PDO methods.

A:

Use something like this:
function vraagOp($table,$where)
{
    static $rVraagOp = null;
    if(!isset($rVraagOp)){
        $qVraagOp = "SELECT * FROM $table WHERE $where";
        $rVraagOp = mysql_query( $qVraagOp );
    }
    $ret = mysql_fetch_assoc( $rVraagOp );
    if(!$ret) $rVraagOp = null;
    return $ret;
}

It's ugly, but if you want like that...

