gpt4 book ai didi

php - 如何使用 PHP 和 MySql 在条件检查中使用 SELECT 和 WHERE & AND

转载 作者:行者123 更新时间:2023-11-29 21:16:47 24 4
gpt4 key购买 nike

在我的服务器上,我试图在数据库表中查找特定字符串,如果找到该字符串,我想检查同一行的另一个字段中的整数值是什么,并更新该整数(如果是)需要时,或退出 PHP 脚本。

下面的代码只是我尝试过的一些代码。我没有看到命令有什么问题,并且运行/调用时没有生成错误消息。

发生的情况是,如果找到该字符串,脚本会自动运行 $there 查询。

我需要做什么才能使其正常工作?

非常感谢。

// This script checks to see if a member name sent by the page exists in the database.

//-------------------------------------------------------------

// The database section starts here.
$servername = "localhost";
$username = "manager";
$password = "********";
$dbname = "golf_ledger";

//------------------------------

// Make a connection with the server.
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check the connection.
if($conn === false){
die("ERROR: Couldn't connect. " . mysqli_connect_error());
}
else {
echo "The connection worked."."<br>"."<br>";
}

//------------------------------------------------------------------

// This is the test string to be searched for.
$memName = "Richardson";

//----------------------------------------

// Populate $result with the search query.
// Database name Table name
$result = mysqli_query($conn,"SELECT * FROM `golf_ledger`.`member_table` WHERE `member_table`.`name` = '$memName'");

if(mysqli_num_rows($result) == 0) {
echo "Sorry, the name was not found";

die();

}

//----------------------------------------

// Something is wrong with this one, possibly.

$there = mysqli_query($conn,"SELECT * FROM `golf_ledger`.`member_table` WHERE `member_table`.`name` = '$memName' AND `member_table`.`pay_Status` = 1");

// "if ($there)" is the same as "if ($there == true)" in PHP.

if ($there == true) {
echo "The name has been found, and they have paid.";

die();

}

//----------------------------------------

$notThere = mysqli_query($conn,"SELECT * FROM `golf_ledger`.`member_table` WHERE `member_table`.`name` = '$memName' AND `member_table`.`pay_Status` = 0");

if ($notThere == true) {
mysqli_query($conn,"UPDATE `golf_ledger`.`member_table` SET `pay_Status` = 1 WHERE `member_table`.`name` = '$memName'");

echo "The name has been found, they have NOT paid, but the status has been updated.";

die();

}

最佳答案

代替此代码:

if ($there == true) {
echo "The name has been found, and they have paid.";

die();

}

尝试一下:

// Check if found any records
if (mysqli_num_rows($there) > 0) {
echo "The name has been found, and they have paid.";

die();

}

关于php - 如何使用 PHP 和 MySql 在条件检查中使用 SELECT 和 WHERE & AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874827/

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