這裏有兩個工作例子,這將讓你去。
您將使用num_rows
使用mysqli_ *函數與預處理語句得到更好的結果:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";
$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
die('Connection failed [' . $db->connect_error . ']');
}
// $passkey = $_GET['ref'];
$passkey = mysqli_real_escape_string($db,$_GET['ref']);
$tbl_name = "yourtable";
// $query = "SELECT * FROM $tbl_name WHERE confirmCode=?";
$query = "SELECT confirmCode FROM $tbl_name WHERE confirmCode=?";
if ($stmt = $db->prepare($query)){
$stmt->bind_param("s", $passkey);
if($stmt->execute()){
$stmt->store_result();
if ($stmt->num_rows == 1){
echo "Code verified.";
exit;
}
else{
echo "Sorry.";
// uncomment below and delete the above echo
// header("HTTP/1.1 404 Not Found");
// header("Location: 404.php");
// exit;
}
}
}
使用mysql_ *功能:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$connect = mysql_connect("xxx","xxx","xxx") or die("Error Connecting To MYSQL Server");
mysql_select_db("xxx") or die("Error connecting to database");
// $passkey = $_GET['ref'];
$passkey = mysql_real_escape_string($_GET['ref']);
$tbl_name = "yourtable";
$checkKey = "SELECT * FROM $tbl_name WHERE confirmCode ='$passkey'";
$confirmKey = mysql_query($checkKey);
if (mysql_num_rows($confirmKey)) {
echo "Code verified.";
}
else{
echo "Sorry.";
// uncomment below and delete the above echo
// header("HTTP/1.1 404 Not Found");
// header("Location: 404.php");
// exit;
}
腳註:
mysql_*
功能已取消通知:
http://www.php.net/manual/en/intro.mysql.php
這個擴展不贊成PHP 5.5.0,並且不建議用於編寫新的代碼,因爲它會將來刪除。相反,應使用mysqli或PDO_MySQL擴展名。請參閱MySQL API Overview以獲取進一步幫助,同時選擇MySQL API。
這些函數允許您訪問MySQL數據庫服務器。有關MySQL的更多信息,請參見»http://www.mysql.com/。
有關MySQL的文檔可以在»http://dev.mysql.com/doc/找到。
[爲什麼不應該在PHP中使用mysql_ *函數?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil
是這個確切的代碼?因爲'$ tbl_name'沒有定義。 – user1978142
它是應用程序的一部分,我們應該爲您考慮? –