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
开发者_Python百科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?
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.
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...
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 it like this:
$r = null;
while (vraagOp("testtable","testtype = test", $r))
{
echo "testing <br>";
}
It is still ugly, but a little bit better.
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>";
}
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.
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.
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);
}
精彩评论