gpt4 book ai didi

php - 使用 php 从 MySql 获取一个整数

转载 作者:行者123 更新时间:2023-11-28 23:58:43 25 4
gpt4 key购买 nike

我是 php 的新手,我在学校的最后一年项目中遇到了一些问题。我在使用 php 从 Select 语句中检索整数时遇到问题。我有 2 个表:

餐 table 露营:

capacity(int 11)
spot_nb (int 11)
location (varchar 20)
availability (varchar 1) //this can be 'Y' or 'N'

订位:

reservation_nb (int 10 auto_increment)
spot_nb (int 11)
uin (varchar 8; null)
persons_nb (int 5)
price (int 11)
email (varchar 60)

对于 php,我有以下代码:

$query5 = $conn->prepare("SELECT MAX(spot_nb) from camping where capacity=2 and availability='Y'");
$query5->execute();
$result = $query5->fetch(PDO::FETCH_ASSOC);
$id = (int) $result;

使用此代码从餐 table 露营中获取最大值并插入预订

$query6 = $conn->prepare("INSERT INTO reservation (price, persons_nb, email, spot_nb) VALUES (:price, 2, :email, :id);");
$query6-> bindParam(':price', $campingNumber);
$query6-> bindParam(':email', $email);
$query6-> bindParam(':id', $id);

但是每次执行query6时,插入的spot_nb都是1。

谢谢,亚历克斯。

最佳答案

使用fetchColumn()而不是 fetch()

$query5 = $conn->prepare("SELECT MAX(spot_nb) from camping 
where capacity=2 and availability='Y'");
$query5->execute();
$result = $query5->fetchColumn();
$id = (int) $result;

关于php - 使用 php 从 MySql 获取一个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30710792/

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