简体   繁体   English

PHP,MySQL准备的语句-您可以通过调用data_seek(0)多次使用执行结果吗?

[英]PHP, MySQL prepared statements - can you use results of execute more than once by calling data_seek(0)?

I have a case where I want to use the results of a prepared statement more than once in a nested loop. 我有一种情况,我想在嵌套循环中多次使用准备好的语句的结果。 The outer loop processes the results of another query, and the inner loop is the results of the prepared statement query. 外循环处理另一个查询的结果,内循环是准备好的语句查询的结果。 So the code would be something like this (just "pseudoish" to demonstrate the concept): 因此,代码将是这样的(只是“伪”来说明概念):

// not showing the outer query, it is just a basic SELECT, not prepared statement
// we'll call it $outer_query

$obj_array = array();   // going to save objects in this
$ids = array(18,19,20); // just example id numbers

$query = "SELECT field1, field2 FROM table1 WHERE id=?";
$stmt = $db->prepare($query);

foreach ($ids as $id) {
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->bind_result($var1, $var2);
    $stmt->store_result(); // I think I need this for data_seek

    while ($q1 = $outer_query->fetch_object()) {
        while ($stmt->fetch()) {
            if ($q1->field1 == $var1) { // looking for a match
               $obj = new stdClass();
               $obj->var1 = $var1;
               $obj->var2 = $var2;

               $obj_array[] = $obj;
               $stmt->data_seek(0); // reset for outer loop
               break;               // found match, so leave inner                
            }
        }
    }
}

The problem I seem to be experiencing is that the values are not getting bound in the variables as I would expect after the first time I use fetch in the inner loop. 我似乎遇到的问题是,值没有像我第一次在内部循环中使用fetch之后所期望的那样绑定在变量中。 Specifically, in one example I ran with 3 ids for the foreach, the first id was processed correctly, the second was processed incorrectly (matches were not found in the inner loop even though they existed), and then the third was processed correctly. 具体来说,在一个示例中,我为foreach运行了3个id,第一个id正确处理,第二个id处理不正确(即使存在,也没有在内环中找到匹配项),然后第三个正确处理。

Is there something wrong with the prepared statment function calls in the sequence I am doing above, or is this an invalid way to use the results of the prepared statement? 按照我在上面做的顺序执行的预处理语句函数有什么问题吗,或者这是使用预处理语句结果的无效方法吗?

Thanks. 谢谢。

You'd better use JOIN to make it with single query and no loops 您最好使用JOIN使其具有单个查询且没有循环

And you don't need any data_seek anyway. 而且您也不需要任何data_seek。

Convert the mysql result to an array, and then you can use array functions to navigate through the array. 将mysql结果转换为数组,然后可以使用数组函数在数组中导航。 Data_seek isn't very elegant, IMO. IMO,Data_seek不是很好。

I found where this code was going wrong. 我发现此代码出了问题。 It had nothing to do with my use of the prepared statement functions, just the placement of the data_seek. 这与我使用预准备语句函数无关,仅与data_seek的位置无关。 I sought help from SO a little too soon :) 我太早地寻求SO的帮助了:)

The loop code that now works exactly as I wanted is as follows: 现在可以完全按照我的需要工作的循环代码如下:

while ($q1 = $outer_query->fetch_object()) {
    while ($stmt->fetch()) {
        if ($q1->field1 == $var1) { // looking for a match
            $obj = new stdClass();
            $obj->var1 = $var1;
            $obj->var2 = $var2;
            $obj_array[] = $obj;
            break;               // found match, so leave inner                
        }
    }
    $stmt->data_seek(0); // reset for outer loop
}
// this was always here, forgot it in original post
$outer_query->data_seek(0) // reset for next iteration of foreach

When I was calling data_seek inside the conditional to reset the prepared statment results, the reset was occurring only if a match was found. 当我在条件内调用data_seek重置准备好的语句结果时,仅在找到匹配项时才发生重置。 This caused the outer loop to run in specific situations with the prepared statement result not reset. 这导致外循环在特定情况下运行,且准备好的语句结果未重置。 Sometimes the forest is hidden by the trees ;) 有时森林被树木所掩盖;)

So, apparently you can easily process prepared statement results multiple times using data_seek, just as I had hoped. 因此,显然可以像我希望的那样,使用data_seek轻松地多次处理准备好的语句结果。

Thanks. 谢谢。

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

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