views:

177

answers:

7

Hi, i got an answer on an older question wich 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: 

I assume you want to iterate over the values you receive from the database?

You should change your loop to a foreach function:

foreach (vraagOp("testtable","testtype = test") as $row) 
{
   // here you have full access on the rows the function returns
   print_r($row);
   echo "testing <br>";
}
Cassy
A: 

Well presumably you can try this:

function do_query($table, $where){
   // please do some escaping for your $table and $where if necessary
   $qVraagOp = "SELECT * FROM `$table` WHERE $where";
   $rVraagOp = mysql_query( $qVraagOp );
   return $rVraagOp;
}

function do_fetch($result){
   return mysql_fetch_assoc( $result );
}

$res = do_query('testtable', 'testtype = "test"');

while($row = do_fetch($res)){
   var_dump($row); // dump each row out
}

My guess is that you have an error in your query at the "testtype = test" because test is a string (or is that a column?) Therefore it was only called once only to find an error.

thephpdeveloper
Humm... why would you want a function `do_fetch` that is basically an alias of `mysql_fetch_assoc` ?
Cassy
+3  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;
}

its ugly but if you want like that....

useless
+1  A: 

you could use something like this instead, would be more nice:

function vraagOp($table,$where, &$resource)
{
    if(!isset($resource)){
        $qVraagOp = "SELECT * FROM $table WHERE $where";
        $rVraagOp = mysql_query( $resource );
    }
    $ret = mysql_fetch_assoc( $resource );
    if(!$ret) $resource = null;
    return $ret;
}

and use like this:

$r = null;
while (vraagOp("testtable","testtype = test", $r)) 
{
   echo "testing <br>";
}

still ugly but a little more better.

useless
+4  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.

nickf
+1 for recommending PDO
outis
A: 

As nickf mentions, PDO has much to offer. Since PDOStatement implements the Traversable interface, you can use it directly in a foreach.

$query = $db->prepare("SELECT id, name, location FROM `events` WHERE `when`=?");
$query->execute(array(strtotime('-3 days UTC')));
foreach ($query as $event) {
   ...
}

PDO also supports prepared statements, which offer efficiency and security that the old mysql driver lacks.

As it stands, the vraagOp looks to be a poor design for a data access layer.

outis
A: 

The answer I gave to your last question (which you didn't accept...) solves this problem.

It maintains a mapping of specific table/where clauses, and uses the correct resource for each call.

function vraagOp($table, $where)
{
    // Holds our mysql resources in a map of "{$table}_{$where}" => resource
    static $results = array();

    $key = $table . '_' . $where;

    if (!isset($results[$key]))
    {
        // first call of this particular table/where
        $results[$key] = mysql_query("SELECT * FROM $table WHERE $where");
    }

    $row = mysql_fetch_assoc($results[$key]);

    if ($row === false)
        // remove this key so a subsequent call will start over with a new query
        unset($results[$key]);

    return $row;
}

// Usage

while ($row = vraagOp("table1", "where field > 7")) {
   print_r($row);
}
meagar