SQL更新后,字段设置为0 [PHP/MySQL]
好吧,我现在病了,所以也许答案就在我面前,但过去 4 个小时我一直坐在这里试图找出为什么会发生这种情况。
基本上我有一个脚本应该更新数据库中的一行。 我试图更新的字段是“名称”,它看起来像这样:
// Updating the database with the new name
$name = $this->db->slashes( $this->friends[$row->key] );
$sql = "UPDATE `friends` SET `name` = '" . $name . "' AND `status` = 'updated' WHERE `key` = " . $row->key . " LIMIT 1";
echo '-- Query: ' . $sql . '<br />';
$result2 = $this->db->query( $sql );
if( !$result2 )
echo mysql_error();
输出变成这样(示例):
-- Query: UPDATE `friends` SET `name` = 'Andrew Johnsson' AND `status` = 'updated' WHERE `key` = 7823583 LIMIT 1
它没有生成任何 mysql_error,因此查询似乎已正确执行,并且从查询的外观来看,它应该刚刚更新数据库中的一行,其中键是数字,并将新名称设置为 Andrew Johnsson 并将状态设置为已更新。
然而!更新后查看数据库后,该行的名称设置为“0”。它到底为什么这么做?
关于为什么会发生这种情况有什么想法吗? 另外,请告诉我您是否需要更多信息来帮助我,我会很乐意提供!
Okay, I'm currently sick so maybe the answer is right in front of me but I've been sitting here for the past 4 hours trying to figure out why this is happening.
Basically I have a script that is supposed to update a row in the database.
The field I'm trying to update is 'name' and it looks like this:
// Updating the database with the new name
$name = $this->db->slashes( $this->friends[$row->key] );
$sql = "UPDATE `friends` SET `name` = '" . $name . "' AND `status` = 'updated' WHERE `key` = " . $row->key . " LIMIT 1";
echo '-- Query: ' . $sql . '<br />';
$result2 = $this->db->query( $sql );
if( !$result2 )
echo mysql_error();
Output becomes this(an example):
-- Query: UPDATE `friends` SET `name` = 'Andrew Johnsson' AND `status` = 'updated' WHERE `key` = 7823583 LIMIT 1
It did not generate any mysql_error so the query seem to have gone through properly and by the looks of the query, it should have just updated a row in the database where the key was a number and set the new name to Andrew Johnsson aswell as setting status to updated.
However! After looking in the database after this update, the name for this row is set to '0'. Why on earth did it do that?
Any ideas as to why this is happening?
Also, tell me if you need some more information to be able to help me and I'll kindly provide it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 UPDATE 语法不正确。它应该是 SET name = 'string', status = 'updated' WHERE key...
目前,您实际上要求它评估 AND 以确定名称列应更新的内容,这就是您得到的原因0.
Your UPDATE syntax is incorrect. It should be SET name = 'string', status = 'updated' WHERE key...
Currently you are literally asking it to evaluate the AND's in order to determine what the name column should be updated with, which is why you're getting the 0.
好吧,这可能太傻了,但是
name
是一个字符串字段,对吗?如果将其设置为任何类型的整数字段,则确实会发生这种情况:字符串被解析为0
。不过,我敢打赌事情比这更复杂一点。
Well, this might be too silly, but
name
is a string field, right? If it's set to any kind of integer field, this is exactly what would happen: the string is parsed as0
.I bet it's something a wee bit more complicated than that, though.