这个MySQL查询有什么问题?

时间:2021-09-05 01:02:06

It's 12:30am and I have been coding for 9 hours straight. I really need to get this project done, but MySQL is messing with my deadline. Could you examine this snippet for me and see if you can find out what is wrong?

现在是上午12:30,我连续编码了9个小时。我真的需要完成这个项目,但MySQL正在搞乱我的截止日期。你可以为我检查这个片段,看看你是否能找出问题所在?

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]
You have an error in your SQL syntax; 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错误[2010年10月6日晚上11:31 CDT]您的SQL语法出错;查看与您的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:
Here is the whole affected code. Keep in mind that this is all in a class. If I'm missing something, let me know.

编辑2:这是受影响的整个代码。请记住,这一切都在课堂上。如果我遗失了什么,请告诉我。

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); }
}

3 个解决方案

#1


7  

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之后删除*,它应该没问题。

#2


0  

Try this:

尝试这个:

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

#3


0  

Check if you are using the ' character or the ´ character (the last one is an accent)

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

#1


7  

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之后删除*,它应该没问题。

#2


0  

Try this:

尝试这个:

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

#3


0  

Check if you are using the ' character or the ´ character (the last one is an accent)

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