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

时间:2016-03-08 18:10:57

标签: php mysql

在我的服务器上,我试图在数据库表中找到一个特定的字符串,如果找到该字符串,我想检查同一行的另一个字段中的整数值是什么,如果它是UPDATE整数需要,或退出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();

}

1 个答案:

答案 0 :(得分:0)

而不是这段代码:

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();

}