简体   繁体   English

PHP:我只能使用一次此功能(在while循环中使用它)

[英]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? 为了使其能够多次工作并且仍在while循环中工作,我必须更改什么?

The error is because you're not resetting the mysql result. 该错误是因为您没有重置mysql结果。 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. 甚至更好,请看一下PDO方法。

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函数:

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. 我的猜测是您在“ testtype = test”中的查询中有错误,因为test是一个字符串(或那是一列?),因此仅被调用一次才发现错误。

As nickf mentions, PDO has much to offer. 尼克(Nickf)提到,PDO提供了很多东西。 Since PDOStatement implements the Traversable interface, you can use it directly in a foreach . 由于PDOStatement实现了Traversable接口,因此可以在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. PDO还支持准备好的语句 ,这些语句提供了旧版mysql驱动程序所缺乏的效率和安全性。

As it stands, the vraagOp looks to be a poor design for a data access layer. 就目前而言, vraagOp对于数据访问层vraagOp似乎是一个较差的设计。

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. 它维护特定表/ where子句的映射,并为每个调用使用正确的资源。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM