我有一些存储过程,一旦成功结束:
select 1 as outcome;
所以我知道它成功了。
在PHP中,我的代码是这样的:
if ($stmt = mysqli_prepare($con, "call storedProc(?)")) {
mysqli_stmt_bind_param($stmt, 'i', $count);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
$outcome = $row['outcome'];
if ($outcome == 1) {
if ($stmt = mysqli_prepare($con, "call secondStoredProc(?)")) {
xxx();
现在的问题是,即使$outcome
为1,每次都会运行xxx();
。我知道这是因为函数xxx
不是一个有效的函数,我在控制台中没有错误。
奇怪的是,只有下一次尝试运行失败的mysqli_prepare()
。请注意,call secondStoredProc(?)
没有任何根本性的错误,如果我在mysqli_prepare()
之前重新连接,否则会失败,那么它可以正常工作,但是:
mysqli_prepare()
之间重新连接MySQL。我找不到任何关于在运行另一个语句之前正确关闭预准备语句的参考(在文档或任何其他有关mysqli的指南中)。除了重新连接之外,我也找不到任何方法这样做,甚至假设这在任何方面都是问题的本质。
请帮忙。
答案 0 :(得分:1)
检查一下......希望这有助于实现你想要做的事情。
这里有一些DB Connect文件
define("HOST", "yo.ur.ip.addr"); // The host you want to connect to.
define("USER", "myfunctionalaccount"); // The database username.
define("PASSWORD", "superdoopersecurepassword!"); // The database password.
define("DATABASE", "thegoods");
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if ( $mysqli->connect_error ) {
die('Connect Error: ' . $mysqli->connect_error);
}
这里的一些函数PHP
function somecoolfunction($someparameter)
if ($stmt = $mysqli->prepare("call storedProc(?)") {
$stmt->bind_param($someparameter);
$stmt->execute();
$stmt->bind_result($outcome);
$stmt->fetch();
if ($stmt->num_rows == 1) {
if ($substmt = $mysqli->prepare("call secondStoredProc(?)") {
$substmt->bind_param($outcome);
$substmt->execute();
$substmt->bind_result($newoutcome);
$substmt->fetch();
xxx();
$mysqli->query(" "); // some other random query
echo $substmt->affected_rows.' rows were updated based on '.$outcome.'.';
} // $substmt closes mysqli connection here
} else {
//$stmt results return 0 or more than 1 result
}
} // $stmt closes mysqli connection here
} // end of function
// You can use this method if you're considering on using w/o the if loops.
$stmt = $mysqli->prepare("call storedProc(?)") //preparing w/o if statement
$stmt->bind_param($someparameter);
$stmt->execute();
$stmt->bind_result($outcome);
$stmt->fetch();
$stmt->close(); //must close since it's not closed conditionally with an if statement.
$stmt = $mysqli->prepare("call secondStoredProc(?)")
$stmt->bind_param($outcome);
$stmt->execute();
$stmt->bind_result($newoutcome);
$stmt->fetch();
$stmt->close();
echo "Stored Proc Outcome: ".$outcome."\nSecond Stored Proc Outcome: ".$newoutcome;