如何检查数据库中的值是否与PHP中的字符串相同

时间:2016-05-01 10:56:52

标签: php html

我想知道如何检查是否存在,但检查是否相同。这是我的代码:

$user = $_COOKIE["c_user"];
$ip = $_SERVER['REMOTE_ADDR'];
$salt = $_COOKIE["c_salt"];
$chk_salt = mysql_fetch_array(mysql_query("SELECT * FROM `table1` WHERE `Salt`='$salt'"));
if ($chk_salt == '0') {
  die("Get out!");
}
else {
  echo "Welcome ".ucwords($user);
}

1 个答案:

答案 0 :(得分:0)

$host   = 'your_mysql_host';
$dbname = 'your_db_name';
$user   = 'username';
$pass   = 'password';

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e)   {
    echo 'Error: ' . $e->getMessage().'<br>';  
}

$user = $_COOKIE["c_user"];
$ip = $_SERVER['REMOTE_ADDR'];
$salt = $_COOKIE ['c_salt'];
$chckSalt = $conn->prepare("SELECT * FROM table1 WHERE Salt=:salt");
$chckSalt->bindValue(':salt', $salt, PDO::PARAM_STR);
$chckSalt->execute ();
if ($chkSalt->rowCount () == 0) {
  die("Get out!");
}
else {
  echo "Welcome ".ucwords($user);
}