1

是否可以使用具有不同绑定结果的语句两次?示例:第一个脚本包含第二个脚本。在 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 重用语句对象来进行新的选择并限制结果。对于输出,该语句需要输出的其他绑定结果。

// 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();

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

谢谢大家

4

0 回答 0