PHP 选择后立即删除
我有一个 PHP 服务器脚本,它从 MySQL 数据库中选择一些数据。
一旦我将 mysql_query 和 mysql_fetch_assoc 的结果存储在我自己的局部变量中,我就想删除我刚刚选择的行。
这种方法的问题在于,PHP 似乎对我的局部变量进行了按引用传递而不是按值传递,并且我的局部变量在删除命令后变得未定义。
有办法解决这个问题吗?这是我的代码:
$query="SELECT id, peerID, name FROM names WHERE peer = $userID AND docID = '$docID' AND seqNo = $nid";
$result = mysql_query($query);
if (!$result)
self::logError("FAIL:1 getUsersNamesUpdate() query: ".$query."\n");
if (mysql_num_rows($result) == 0)
return array();
$row = mysql_fetch_assoc($result);
$result = array();
$result["id"] = $row["id"];
$result["peerID"] = $row["peerID"];
$result["name"] = $row["name"];
$query="DELETE FROM names WHERE id = $result[id];";
$result = mysql_query($query);
if (!$result)
self::logError("FAIL:2 getUsersNamesUpdate() query: ".$query."\n");
return $result;
I have a PHP server script that SELECTs some data from a MySQL database.
As soon as I have the result from mysql_query and mysql_fetch_assoc stored in my own local variables, I want to delete the row I just selected.
The problem with this approach is that it seems that PHP has done pass-by-reference to my local variables instead of pass-by-value, and my local variables become undefined after the delete command.
Is there anyway to get around this? Here is my code:
$query="SELECT id, peerID, name FROM names WHERE peer = $userID AND docID = '$docID' AND seqNo = $nid";
$result = mysql_query($query);
if (!$result)
self::logError("FAIL:1 getUsersNamesUpdate() query: ".$query."\n");
if (mysql_num_rows($result) == 0)
return array();
$row = mysql_fetch_assoc($result);
$result = array();
$result["id"] = $row["id"];
$result["peerID"] = $row["peerID"];
$result["name"] = $row["name"];
$query="DELETE FROM names WHERE id = $result[id];";
$result = mysql_query($query);
if (!$result)
self::logError("FAIL:2 getUsersNamesUpdate() query: ".$query."\n");
return $result;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将使用第二条语句覆盖您的
$result
变量:将名称更改为其他名称。它与引用调用等无关。
实际上,您第一次分配值是不必要的,因为
$row
已经是一个数组:您可以这样做:
然后您甚至不必更改第二条语句的变量名称。但请考虑使用有意义的变量名称。
You are overwriting your
$result
variable with your second statement:Change the name to something else. It has nothing to do with call-by-reference or such.
Actually, your first assignment of the values is unnecessary as
$row
is already an array:You could just do:
Then you don't even have to change your variable name for the second statement. But consider to use meaningful variable names.
首先,为什么不只使用一个查询来
删除
您感兴趣的行呢?我想这样的事情应该可以解决问题:
当然,不要忘记转义/转换应该是的值;-)
这样,就不需要
选择 查询,后跟一个
删除
查询。第二:为了使您的代码更易于阅读/理解/维护,您可能不应该将同一变量重复用于多个不同的目的。
在这里,您的 $result 变量用于不止一件事,这使得事情变得更难理解:
mysql_query
返回的资源这有点令人困惑,并且有一天或另一天会导致错误......
实际上,它已经有了;-):第三个分配将覆盖您使用第二个分配获得的数据,并且繁荣,您已经丢失了与您的行相对应的信息刚刚删除;-)
First of all, why not just use only one query to
delete
the row that interests you ?Something like this should do the trick, I suppose :
Of course, don't forget to escape/convert the values that should be ;-)
This way, no need for a
select
query, followed by adelete
one.Second : to make your code more easier to read / understand / maintain, you should probably not re-use the same variable for several different purposes.
Here, your
$resul
t variable is used for more than one thing, and it makes things harder to understand :mysql_query
mysql_query
It's a bit confusing, and will, one day or another, lead to errors...
Actually, it already has ;-) : the third assignment is overriding the data you're getting with the second ones, and boom, you've lost the information that corresponds to the row you've just deleted ;-)