简体   繁体   English

这个MySQL查询有什么问题?

[英]What is wrong with this MySQL Query?

It's 12:30am and I have been coding for 9 hours straight. 现在是上午12:30,我连续编码了9个小时。 I really need to get this project done, but MySQL is messing with my deadline. 我真的需要完成这个项目,但MySQL正在搞乱我的截止日期。 Could you examine this snippet for me and see if you can find out what is wrong? 你可以为我检查这个片段,看看你是否能找出问题所在?

PHP/MySQL Query PHP / MySQL查询

$q = $this->db->query("SELECT * FROM bans WHERE ip='".$ip."'");

Keeps returning the following error... 继续返回以下错误...

MYSQL Error [Oct 6th, 2010 11:31pm CDT] MYSQL错误[2010年10月6日晚上11:31 CDT]
You have an error in your SQL syntax; 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM bans WHERE ip='206.53.90.231'' at line 1 (1064) 查看与您的MySQL服务器版本对应的手册,以便在第1行(1064)'* FROM禁止WHERE ip ='206.53.90.231'附近使用正确的语法

I do not see anything wrong with the query. 我没有看到查询有任何问题。 I've even tried different methods of including the variable $ip but with no avail. 我甚至尝试过包含变量$ ip的不同方法,但没有用。

EDIT: 编辑:
Just to add in here, the ip column in my database is a varchar(255). 只是在这里添加,我的数据库中的ip列是varchar(255)。

EDIT 2: 编辑2:
Here is the whole affected code. 这是整个受影响的代码。 Keep in mind that this is all in a class. 请记住,这一切都在课堂上。 If I'm missing something, let me know. 如果我遗失了什么,请告诉我。

Line from another Function 来自另一个函数的行

if($this->isBanned($_SERVER['REMOTE_ADDR'])===true) { return json_encode(array('error'=>'You are banned from this ShoutBox.')); }

Affected Function 受影响的功能

function isBanned($ip) {
    $q = $this->db->query("SELECT * FROM bans WHERE ip='".$ip."'"); $num = $this->db->affected_rows;
    if($num>0) { $row = $this->db->fetch_array($q); if(($row['expires'] < time()) && ($row['expires'] !== 0)) { $this->unbanUser($ip,'internal'); return false; } return true; } return false;
}

unbanUser function unbanUser功能

function unbanUser($ip,$t='box') {
    $q = $this->db->query("SELECT * FROM bans WHERE ip='".$ip."'"); $num = $this->db->affected_rows; if($num>0) { $q = $this->db->query("DELETE * FROM bans WHERE ip='".$ip."'"); 
    return (($t=='box') ? json_encode(array('status'=>'removed')) : true); } else { return (($t=='box') ? json_encode(array('error'=>'Unable to locate the user.')) : true); }
}

I think it may be It is your DELETE statement which is causing the error. 我认为可能 是你的DELETE语句导致错误。

Remove the * after the DELETE and it should be fine. DELETE之后删除* ,它应该没问题。

试试这个:

$q = $this->db->query('SELECT * FROM bans WHERE ip="' . $ip . '"');

检查您是否使用'字符或'字符(最后一个是重音)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM