PHP 选择后立即删除

发布于 2024-08-29 18:50:40 字数 932 浏览 0 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

少跟Wǒ拽 2024-09-05 18:50:40

您将使用第二条语句覆盖您的 $result 变量:

$query="DELETE FROM names WHERE id = $result[id];";
$result = mysql_query($query); // result does not contain the array anymore

将名称更改为其他名称。它与引用调用等无关。


实际上,您第一次分配值是不必要的,因为 $row 已经是一个数组:

$row = mysql_fetch_assoc($result);
$result = array();
$result["id"] = $row["id"];
$result["peerID"] = $row["peerID"];
$result["name"] = $row["name"];

您可以这样做:

$row = mysql_fetch_assoc($result);
// at the end
return $row;

然后您甚至不必更改第二条语句的变量名称。但请考虑使用有意义的变量名称。

You are overwriting your $result variable with your second statement:

$query="DELETE FROM names WHERE id = $result[id];";
$result = mysql_query($query); // result does not contain the array anymore

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:

$row = mysql_fetch_assoc($result);
$result = array();
$result["id"] = $row["id"];
$result["peerID"] = $row["peerID"];
$result["name"] = $row["name"];

You could just do:

$row = mysql_fetch_assoc($result);
// at the end
return $row;

Then you don't even have to change your variable name for the second statement. But consider to use meaningful variable names.

吾性傲以野 2024-09-05 18:50:40

首先,为什么不只使用一个查询来删除您感兴趣的行呢?

我想这样的事情应该可以解决问题:

delete 
from names
where peer = $userID 
  AND docID = '$docID' 
  AND seqNo = $nid

当然,不要忘记转义/转换应该是的值;-)

这样,就不需要选择 查询,后跟一个删除 查询。

第二:为了使您的代码更易于阅读/理解/维护,您可能不应该将同一变量重复用于多个不同的目的。

在这里,您的 $result 变量用于不止一件事,这使得事情变得更难理解:

  • 第一个 mysql_query 返回的资源
  • ,然后,包含来自 然后,第一行
  • ,第二个 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 :

delete 
from names
where peer = $userID 
  AND docID = '$docID' 
  AND seqNo = $nid

Of course, don't forget to escape/convert the values that should be ;-)

This way, no need for a select query, followed by a delete 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 $result variable is used for more than one thing, and it makes things harder to understand :

  • resource returned by the first mysql_query
  • then, array containing data from the first row
  • then, resource returned by the second 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 ;-)

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