gpt4 book ai didi

php - 使用 NULL 作为输入参数在 PHP 中执行多个查询

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

编辑 (2011-07-23)

得到了一些非常有用的答案,这两个我都尝试过实现。但我似乎无法从我的 Get_Security 语句中取回 id。我很确定我的问题是,在我的第一个调用语句 Get_Security 中,最后三个参数设置为 NULL。好像其他人也有同样的问题。 似乎没有太多关于将 NULL 作为输入的文档。如何解决这个问题?

新代码

$stmt = mysqli_stmt_init($link);
$sql = "CALL Get_Security('$symbol', '$tagName', NULL, NULL, NULL)";
if (!mysqli_stmt_prepare($stmt, $sql)){
$error = 'Failed to prepare statement. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
include '../error.html.php';
exit();
}
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
}
mysqli_stmt_close($stmt);
mysqli_close($link);

include $_SERVER['DOCUMENT_ROOT'] . 'mypath-to-database-link'; //this gets $link
$stmt = mysqli_stmt_init($link);
$sql = "CALL Add_Active('$id','Research')";
if (!mysqli_stmt_prepare($stmt, $sql)){
$error = 'Failed to prepare statement Add_Active. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
include '../error.html.php';
exit();
}
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($link);

include $_SERVER['DOCUMENT_ROOT'] . 'mypath-to-database-link'; //this gets $link
$sql = "INSERT INTO MyTable SET
id='$id',
open_items='$openItems',
attachments='$attachments'
";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $sql)){
$error = 'Failed to INSERT INTO Research_Security. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
include '../error.html.php';
exit();
}
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($link);

原始条目

广泛搜索(例如 PHP 手册、SO 问题)但答案令人困惑。

我需要连续执行 3 个 SQL 语句:

  1. 调用存储过程 Get_Security,它接受一些输入并返回一个数组,包括 ID。

  2. 调用另一个存储过程 Add_Active,它将从 Get_Security 返回的 ID 作为输入。

  3. 将一些变量插入到我的表中。

问题:我收到 MySQL 错误号 2014:“命令不同步;您现在无法运行此命令”。

我知道我必须使用 mysqli_stmt_preparemysqli_stmt_executemysqli_stmt_close 来解决这个问题,但是如何做到这一点非常令人困惑。

非常感谢有关如何使用上述函数翻译它的帮助。

代码:

$sql = "CALL Get_Security('$symbol', '$tagName', NULL, NULL, NULL)";   
$result = mysqli_query($link, $sql);
if (!$result){
$error = 'Error calling stored procedure Get_Security.';
include '../error.html.php';
exit();
}
while($row = mysqli_fetch_array($result)){
$tags[] = array('id' => $row['id']);
}
foreach ($tags as $tag){
$id = $tag['id'];
}


$sql = "CALL Add_Active('$id','Research')";
$result = mysqli_query($link, $sql);
if (!$result){
$error = 'Error calling stored procedure Add_Active. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
include '../error.html.php';
exit();
}

$sql = "INSERT INTO MyTable SET
id='$id',
open_items='$openItems',
attachments='$attachments'
";
if (!mysqli_query($link, $sql)){
$error = 'Error adding submitted tag into Research_Security. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
include '../error.html.php';
exit();
}

最佳答案

希望对您有所帮助。据我所知,您并没有做任何花哨的事情,所以这就足够了。 PDO 也支持存储过程的 IN/OUT 参数,但我没有看到您使用它们。

请注意,PDO 根据其初始化方式以不同方式处理错误。所以我在这里跳过了错误处理。如果您有任何问题,请告诉我。

另请注意,在您添加 DSN(例如 MySQL's)之前,此代码不关心它是什么数据库类型,因此 DSN 可以是一个配置值,使您的代码更具可移植性。我相信您也可以看到如何将此代码轻松扩展为类/模型结构(特别是安全检查 SP 可以成为 PHP 方法)

$db = new PDO(); // http://www.php.net/manual/en/pdo.construct.php for params

// These generate PDO_Statement (see: http://www.php.net/manual/en/class.pdostatement.php)
$securityStmt = $db->prepare("CALL Get_Security( ?, ?, ?, ?, ? )");
$addActiveStmt = $db->prepare("CALL Add_Active( ?, ? )");
$insertStmt = $db->prepare("INSERT INTO MyTable SET id=?, open_items=?, attachments=?");

// Security CALL
$securityStmt->bindParam( 1, $symbol, PDO::PARAM_STR );
$securityStmt->bindParam( 2, $tagName, PDO::PARAM_STR );
$securityStmt->bindParam( 3, NULL, PDO::PARAM_NULL );
$securityStmt->bindParam( 4, NULL, PDO::PARAM_NULL );
$securityStmt->bindParam( 5, NULL, PDO::PARAM_NULL );

$securityStmt->execute();

// Bind the ID to a variable is useful sometimes...
$securityStmt->bindColumn( 'id', $securityId );
$securityStmt->fetch( PDO::FETCH_BOUND );

/*
Insert + Active call
These are much simpler because we don't need to set the data types of the input
(they are all string I hope...you didn't mention what the last 2 were in the insert).
*/

$addActiveStmt->execute(
array(
$securityId,
'Wedge Research'
)
);

$insertStmt->execute(
array(
$securityId,
$openItems,
$attachments
)
);

关于php - 使用 NULL 作为输入参数在 PHP 中执行多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6752124/

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