你不應該使用普通的mysql_query
引述php.net
警告這個擴展不贊成PHP 5.5.0,並且將 在去除未來。應該使用MySQLi或PDO_MySQL擴展 。另請參閱MySQL:有關更多信息,請選擇API指南和相關常見問題 。替代該功能包括: mysqli_query()PDO ::查詢()
使用PDO或mysqli的嘗試。 這對於防止mysql注入至關重要。
http://php.net/manual/en/pdo.prepared-statements.php
這裏是如何與PDO
$config['db'] = array(
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname'
);
try {
$db = new PDO("mysql:host={$config['db']['host']};dbname={$config['db']['dbname']}",
$config['db']['username'], $config['db']['password']);
} catch(PDOException $e) {
echo $e->getMessage();
}
$sql = "SELECT * FROM `users`";
$stmt = $db->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll()
$username = 'Bob';
$email = '[email protected]';
$nickname = 'BobTheMan';
foreach($users as $user)
{
if($user['username'] == $username)
{
// there is a user by this username
}
if($user['email'] == $email)
{
// there is a user by this email
}
if($user['nickname'] == $nickname)
{
// there is a user by this nickname
}
}
來源
2014-02-27 15:35:02
oBo
做到這一點的例子'或'運營商是你的朋友 –
我應該在哪裏使用'或'操作?的 –
可能重複[如何檢查是否值的MySQL數據庫中已存在(http://stackoverflow.com/questions/8015098/how-to-check-if-value-already-exists-in-mysql-database) – Fibonacci