gpt4 book ai didi

php - 从数据库中删除记录并将其保存到文件中 - PHP

转载 作者:行者123 更新时间:2023-11-29 09:46:40 24 4
gpt4 key购买 nike

是的,正如我的问题所问,我需要删除记录,并且我删除的相同记录将它们动态保存在文件中

我只能查看或删除它们

<?php

session_start();

include 'connessione.php';

$id = $_SESSION['id'];

$query_string = "SELECT * FROM utenti ORDER BY id DESC LIMIT 50";
$query = mysqli_query($connessione, $query_string);

?>
<?php

while($row = mysqli_fetch_assoc($query)){ ?>

<?php echo $row['id'] ;?>

<?php } ?>

了解这个过程,我可以删除数据库中的数据,但保留本地副本以防万一

最佳答案

您可以将这些结果保存在 JSON 数组中。

<?php

//Start the session
session_start();

//Include connection
include 'connessione.php';

//Get user ID
$id = $_SESSION['id'];

//Query to get stuff from database
$query_string = "SELECT * FROM utenti ORDER BY id DESC LIMIT 50";
$query = mysqli_query($connessione, $query_string);

//Get results
$results = mysqli_fetch_assoc($query);

//Make that into a JSON array
$results = json_encode( $results );

//Put those results in a file (create if file not exist)
$fileName = 'backup_file_' . time() . '.txt';
$file = fopen( $fileName , 'a' );
fwrite( $file, $results );
fclose( $file );

//Delete the rows that you just backed up
$query_delete = "DELETE FROM utenti ORDER BY id DESC LIMIT 50";
mysqli_query( $connessione, $query_delete );

?>

例如,如果您的表格如下所示:

| id | name | email_address |
|----+------+---------------|
| 1 | dave | dave@test.com |
| 2 | emma | emma@test.com |
| 3 | mark | mark@test.com |
+----+------+---------------+

您的备份文件将如下所示:

[{"id":1,"name":"dave","email_address":"dave@test.com"},{"id":2,"name":"emma","email_address":"emma@test.com"},{"id":3,"name":"mark","email_address":"mark@test.com"}]

关于php - 从数据库中删除记录并将其保存到文件中 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55535985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com