PHP静态计数器?

发布于 2024-11-07 04:56:58 字数 276 浏览 0 评论 0原文

我正在尝试创建一个即使多次调用也能保留计数的脚本。这是为了给脚本写入文件的某些事物行提供唯一的 ID。

例如:

Monday I call the script and make 2 lines. The script gives ID 1 and 2 to those lines.
Friday I call the script again and make another line. The script gives ID 3 to this one.
etc

I'm trying to create a script that will retain a count, even when it is called multiple times. This is to give unique IDs to certain things lines that the script is writing to a file.

For exmaple:

Monday I call the script and make 2 lines. The script gives ID 1 and 2 to those lines.
Friday I call the script again and make another line. The script gives ID 3 to this one.
etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

硬不硬你别怂 2024-11-14 04:56:58

我建议使用 MySQL 的自动增量功能。

否则,存储一个平面文件来存储运行计数(并在使用时锁定它以避免并发问题)。

I would recommend using MySQL's auto increment feature.

Otherwise, store a flat file to store a running count (and lock it when in use to avoid concurrency issues).

坚持沉默 2024-11-14 04:56:58

您不能使用静态变量来做到这一点。如果脚本结束,该变量将失去其值。您可以将信息保存在您想要的任何存储类型中(MySQL、文本文件、xml ...)。

You can't do this using static variables. The variable will lose its value if the scripts end. You could save your information in any storage type you want (MySQL, text file, xml ...).

歌入人心 2024-11-14 04:56:58

读取最后添加的行的 ID 并将其加一怎么样? PHP 对数据类型没有那么严格,所以很容易做类似的事情

$a = "1";
$a = (int)$a;
$a++;
// write $a for next line

How about reading the ID of the last line added and just increasing it by one? PHP isn't that strict with data types, so it's quite easy to do something like

$a = "1";
$a = (int)$a;
$a++;
// write $a for next line
酷遇一生 2024-11-14 04:56:58

如果您尚未与数据库进行交互,那么最适合您的答案可能是 SQLite。 (SQLite 是一个存储在平面文件中的可通过 SQL 访问的数据库,无需持久服务器即可实现基本数据库功能。它可以使用 PDO 在 PHP 中进行访问。https://www.php.net/manual/en/ref.pdo-sqlite.php)

否则,读取文件可能是最简单的方法。有一种更智能、更高效的方法可以使用 fseek 来完成此操作,但对我来说现在弄清楚还为时过早。

<?php
    $file = fopen($theFilename, 'r');
    while ($line = fscanf($file));   //Ugly, but the last value of $line will be the last line of the file. (Super cumbersome if the file is big.)
    $array = explode($line, '|') // Or whatever delimiter you're using to separate data
    $last_id = $array[0] // Assuming the ID is in the first place in the file.
 ?>

If you're not already interacting with a database, the best answer for you might be SQLite. (SQLite is an SQL-accessible database stored in a flat file, allowing basic database functions without the need for a persistent server. It's accessed in PHP using PDO. https://www.php.net/manual/en/ref.pdo-sqlite.php)

Otherwise, reading the file is probably the easiest way. There's an exponentially smarter and more efficient way to do this using fseek, but it's too early for me to figure it out.

<?php
    $file = fopen($theFilename, 'r');
    while ($line = fscanf($file));   //Ugly, but the last value of $line will be the last line of the file. (Super cumbersome if the file is big.)
    $array = explode($line, '|') // Or whatever delimiter you're using to separate data
    $last_id = $array[0] // Assuming the ID is in the first place in the file.
 ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文