这个 MySQL 查询有什么问题?

发布于 2024-09-26 10:20:43 字数 1730 浏览 0 评论 0原文

现在是中午 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所的.畏惧 2024-10-03 10:20:43

我认为可能是 这是您的 DELETE 语句导致了错误。

删除DELETE后面的*就可以了。

I think it may be It is your DELETE statement which is causing the error.

Remove the * after the DELETE and it should be fine.

场罚期间 2024-10-03 10:20:43

试试这个:

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

Try this:

$q = $this->db->query('SELECT * FROM bans WHERE ip="' . $ip . '"');
2024-10-03 10:20:43

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文