gpt4 book ai didi

从两个表中选择 PHP

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:14 25 4
gpt4 key购买 nike

这是我的数据库:

table 1: dog
column 1: email
column 2: password

table 2: cat
column 1: email
column 2: password

我想检查一个字符串或数据是否已经存在于两个数据库的列中:

//check if email exists in either dog or cat.
$stmt = $dbh->prepare("SELECT email FROM dog AND cat WHERE email = :email");
$stmt->bindParam(':email', $email);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

//if the email exists in either dog or cat, echo the "taken" message below
if($row['email'] == $email) {
echo "sorry, the email is taken already!";
}

但它对我不起作用。我怎样才能让它发挥作用?

最佳答案

SQL 语法无效。

我们可以测试 prepare 的返回值,如果它是 FALSE,那么我们就知道发生了错误。

这是一个例子:

  //check if email exists in either dog or cat.
$sql = "SELECT 'dog' AS src, d.email FROM dog d WHERE d.email = :demail
UNION ALL
SELECT 'cat' AS src, c.email FROM cat c WHERE c.email = :cemail";

if ($stmt = $dbh->prepare($sql)) {
$stmt->bindParam(':demail', $email);
$stmt->bindParam(':cemail', $email);
$stmt->execute();
...

} else {
// handle error condition
}

关于从两个表中选择 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313596/

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