我正在尝试使用以下查询从一个表中提取数据,但我需要它来运行第一个查询,如果得到结果则停止,然后如果没有,则运行第二个查询。每次运行搜索时,我都会得到两个结果,但是两个结果之一却在回声没有结果。我还计划添加更多查询,以便可以根据需要返回结果。我已经在这个问题上工作了几天,最终我想寻求帮助。预先感谢。
$conn = new mysqli("localhost", "user", "password", "dbname");
if(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo $conn->host_info . "\n";
$sql = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= curdate()";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
}
}
else {
echo "Not in system1";
}
$sql1 = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1";
$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) >0) {
while($row1 = mysqli_fetch_assoc($result1)) {
echo "Card not active"; }
}
else {
echo "Not in system";
//新添加的代码
$ row = mysqli_fetch_assoc($ result);
如果($ row){
if($ row ['Active']){
回显“徽章编号:”。$ row [“ BadgeNumber”]。 “-名称:”。$ row [“ BadgeName”]。 “
”;
}
其他{
回显“卡无效”;
}
如果($ row){
if($ row ['Barred'])
回声“持卡人被禁止”;
}
如果($ row){
if($ row ['Lost'])
回显“卡已报告丢失”;
}
}
其他{
回显“不在系统中”;
答案 0 :(得分:0)
您将第二条语句放入第一条语句的第一条ELSE中,如下所示:
$conn = new mysqli("localhost", "user", "password", "dbname");
if(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//echo $conn->host_info . "\n";
$sql = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= curdate()";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
}
}
else {
$sql1 = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1";
$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) >0) {
while($row1 = mysqli_fetch_assoc($result1)) {
echo "Card not active"; }
}
else {
echo "Not in system";}
}
答案 1 :(得分:0)
您也许可以将两个查询都放在一个查询中,而只获取找到的第一行?像这样:
$sql = "SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= CURDATE()
UNION
SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1
LIMIT 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row) {
if ($row['Active']) {
echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
}
else {
echo "Card not active";
}
}
else {
echo "Not in system";
}
编辑要将其扩展到N个支票,您可以添加更多
UNION
SELECT ...
$ sql的行,例如
$sql = "SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= CURDATE()
UNION
SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1
UNION
SELECT ...
UNION
SELECT ...
...
LIMIT 1";
由于LIMIT 1,查询将根据需要检查尽可能多的SELECT查询,以返回一行。因此,即使有多个SELECT匹配该人(不确定是否/应该吗?),您也会得到第一个,而不是全部。
或者,您可以简单地获取 all 检查所需的列,然后在后端逻辑中进行操作:
$sql = "SELECT BadgeNumber, BadgeName, Active, ExpirationDate
FROM SCANMSTR
WHERE BadgeNumber LIKE $query
LIMIT 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row) {
if ($row['Active']) {
echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
}
else if ($row['ExpirationDate'] < date('Y-m-d')) {
echo "Card not active";
}
}
else {
echo "Not in system";
}
我个人认为我会这样做,感觉更简洁,重复SQL更少,等等。尽管这两种方法都可以工作,但:)..