这个 MySQL 查询有什么问题?
现在是中午 12:30,我已经连续编码了 9 个小时。我确实需要完成这个项目,但 MySQL 耽误了我的截止日期。你能帮我检查一下这个片段,看看你是否能找出问题所在吗?
PHP/MySQL 查询
$q = $this->db->query("SELECT * FROM bans WHERE ip='".$ip."'");
不断返回以下错误...
MYSQL 错误 [2010 年 10 月 6 日 11:31pm CDT]
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 (1064) 的 '* FROM bans WHERE ip='206.53.90.231'' 附近使用的正确语法
我没有发现查询有任何问题。我什至尝试了包含变量 $ip 的不同方法,但没有效果。
编辑:
在此添加一下,我的数据库中的 ip 列是 varchar(255)。
编辑2:
这是受影响的完整代码。请记住,这一切都在一个类中。如果我遗漏了什么,请告诉我。
来自另一个函数的线路
if($this->isBanned($_SERVER['REMOTE_ADDR'])===true) { return json_encode(array('error'=>'You are banned from this ShoutBox.')); }
受影响的函数
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($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); }
}
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?
PHP/MySQL Query
$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)
I do not see anything wrong with the query. I've even tried different methods of including the variable $ip but with no avail.
EDIT:
Just to add in here, the ip column in my database is a 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.
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
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); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为可能是这是您的DELETE
语句导致了错误。删除
DELETE
后面的*
就可以了。I think it may beIt is yourDELETE
statement which is causing the error.Remove the
*
after theDELETE
and it should be fine.试试这个:
Try this:
检查您是否使用 ' 字符或 ´ 字符(最后一个是重音符号)
Check if you are using the ' character or the ´ character (the last one is an accent)