MySQL查询错误

时间:2015-10-13 01:21:02

标签: mysql mysql-error-1064

我需要查询数据库,该数据库正在检查user_ref中的数字。如果user_ref为5,则拒绝。

代码:

if(!empty($_GET['ref'])){
    $resultt = mysql_query("SELECT `user_id` FROM `users` WHERE `user_name` = '{$_GET['ref']}' AND user_ref < 5 LIMIT 1");
    if(mysql_num_rows($resultt) > 0) {

2 个答案:

答案 0 :(得分:1)

使用<>

SELECT `user_id` FROM `users` WHERE `user_name` = '{$_GET['ref']}' AND user_ref <> 5 LIMIT 1"

答案 1 :(得分:0)

不推荐使用预准备语句mysql_query()。例如PDO:

<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$user_ref = $_GET['ref'];
$stmt = $db->prepare("SELECT user_id FROM users WHERE user_name = :user_ref AND user_ref < 5 LIMIT 1");
$stmt->bindValue(':user_ref', $user_ref, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$row_count = $stmt->rowCount(); // Returned rows..

foreach($rows as $row) {
   // I think this doesn't need any explanation
}
?>