使用不同的绑定结果的语句两次

时间:2012-05-10 11:02:38

标签: mysqli prepared-statement

是否可以使用两次语句与不同的绑定结果? 示例:第一个脚本包含第二个脚本。 在firstscript.php中,预准备语句进行选择,并通过第一个输出的变量绑定结果。输出可以是例如结果量。该脚本包括secondscript.php。

// firstscript.php
$sql ="SELECT field1,field2,field3,field4 FROM table WHERE table.field1=? AND table.field2=?";
$stmt->bind_params("si",$para1,$para2);
$stmt = $con->prepare($sql);
$stmt->bind_result($field1,$field2,$field3,$field4);
$stmt->execute();
$stmt->num_rows();
while($stmt->fetch())
    {
    //output
    }
$stmt->close();
include(secondscript.php);

secondscript.php重用statement-object来结合限制结果进行新选择。对于输出,语句需要输出的其他绑定结果。

// secondscript.php
$limit = " LIMIT 0,5";
//use the same statement from first script in conjunction with $limit
$stmt->prepare($sql.$limit);
$stmt->execute();
/*
// case 1
//without binding results 
while($stmt->fetch())
    {
    //no output
    }

// case 2
//with binding results - like in the first script
$stmt->bind_result($field1,$field2,$field3,$field4);
while($stmt->fetch())
    {
    //output
    }

// case 3
//same statement with *different* bindings
*/
$stmt->bind_result($field3,$field4);
while($stmt->fetch()
    {
    //output - failure; binding error
    //Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
    }
$stmt->close();

如何为最终输出分配重用语句新的绑定结果?

thanx to all

0 个答案:

没有答案