gpt4 book ai didi

Mysql使用准备好的语句选择和插入

转载 作者:行者123 更新时间:2023-11-29 06:52:07 25 4
gpt4 key购买 nike

我的 mysql 查询工作正常

INSERT INTO donor_location (pc_id)
SELECT id
FROM pc
WHERE postcode= ?

即从邮政编码表中获取邮政编码 ID,然后将该 ID 插入 donor_location 表。我正在使用 mysqli 和准备好的语句

没有 select 部分会很简单——就像

$stmt = $mysqli->prepare("INSERT INTO donor_charity(
id) values (?)") ;

但是我完全不知道如何合并选择

最佳答案

你所做的几乎是一样的,只是改变了查询位。
要从 charity_donor 中选择 ID 为 25 的所有记录,您可以执行以下查询:

SELECT *
FROM donor_charity
WHERE id = 25

现在要查询这个,首先你必须准备它:

$stmt = $mysqli->prepare("
SELECT *
FROM donor_charity
WHERE id = ?
");

现在要遍历结果,您必须绑定(bind)参数并执行查询。

$stmt->bind_param('d', 25 ); // First param means the type of the value you're 
passing. In this example, d for digit.
$stmt->execute();

然后你设置一个数组来保存查询返回的数据,

$row = array();
stmt_bind_assoc($stmt, $row);

现在循环返回的数据。

while ( $stmt->fetch () ) {
print_r($row); // Should now contain the column.
}

有关文档,请参阅:
准备:http://www.php.net/manual/en/mysqli.prepare.php
绑定(bind)参数:http://www.php.net/manual/en/mysqli-stmt.bind-param.php
执行:http://www.php.net/manual/en/mysqli-stmt.execute.php
获取:http://www.php.net/manual/en/mysqli-stmt.fetch.php

关于Mysql使用准备好的语句选择和插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14707812/

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