PHP DELETE right after selection

I have a PHP script server that selected some data from a MySQL database.

As soon as I get the result from mysql_query and mysql_fetch_assoc stored in my own local variables, I want to delete the row just selected.

The problem with this approach is that, apparently, PHP cross-referenced my local variables instead of pass-by-value, and my local variables became undefined after the delete command.

Can I 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;
+3
source share
2 answers

You rewrite the variable $resultwith your second statement:

$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;

. .

+7

, delete ?

- , :

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

, / , ; -)

, select, delete.


-: //, , , .

$resul t , :

  • , mysql_query
  • ,
  • , mysql_query

, , -, ...
, ; -): , , , , , ' ; -)

+1

Source: https://habr.com/ru/post/1742613/


All Articles