簡體   English   中英

如何在使用PHP和MySql的條件檢查中將SELECT與WHERE&AND一起使用

[英]How to use SELECT with WHERE & AND in conditional checks using PHP and MySql

在我的服務器上,我嘗試在數據庫表中查找特定的字符串,如果找到了該字符串,我想檢查一下同一行的另一個字段中的整數值,並在需要時更新該整數,或退出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();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM