gpt4 book ai didi

php - mysqli_fetch_assoc 会停止下一个 mysqli_prepare 工作吗?

转载 作者:可可西里 更新时间:2023-11-01 08:44:26 25 4
gpt4 key购买 nike

我有一些存储过程,成功后以:

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() 之前重新连接,否则它会失败,但它可以正常工作,但是:

  1. 我不认为它应该像这样失败。
  2. 我不想在每次 mysqli_prepare() 之间重新连接到 MySQL。
  3. 我需要知道现象的性质,这样我就可以避免它可能导致的其他问题。

我找不到任何引用(在文档或任何其他关于 mysqli 的指南中)需要在运行另一个之前正确关闭准备好的语句。除了重新连接之外,我也找不到任何方法来这样做,甚至假设这在任何方面都是问题的本质。

请帮忙。

最佳答案

检查一下...希望这有助于实现您正在尝试做的事情。

一些数据库连接文件在这里

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;

关于php - mysqli_fetch_assoc 会停止下一个 mysqli_prepare 工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32462783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com