PHP 下一个 MySQL 行 - 如何移动指针直到函数检查为真?

发布于 2024-08-27 01:03:49 字数 198 浏览 7 评论 0原文

我有一个 PHP 脚本,它从 MySQL 数据库中的一行获取一个值,通过一个函数运行它,如果确定为 true,则返回一个值,如果为 false,则需要转到数据库中的下一个值,然后检查该值,直到最终返回 true。 我想我需要使用 mysql_fetch_assoc,但我不太确定以什么方式使用它...我希望我可以将我的代码发布得更具体,但它有很多代码,而且其中大部分与这个问题...

I have a PHP script which takes a value from a row in my MySQL database, runs it through a function, and if it determines it's true returns one value, and if it's false, it needs to go to the next value in the database and check that one until eventually one returns true.
I think I need to use mysql_fetch_assoc, but I'm not really sure in what way to use it... I wish I could post my code to be more specific, but it's a lot of code and most of it has no bearing on this issue...

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

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

发布评论

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

评论(4

烟─花易冷 2024-09-03 01:03:49

您可以在数据库中执行“函数”吗?处理表中的每一行来检查某种类型的条件确实效率很低。这正是数据库所擅长的,即高效处理查询并快速获得答案。

所以我建议看看如何在数据库端完成这一切,以便您的 PHP 代码只是获取最终结果(即由函数过滤的行)。也许如果您提供有关“功能”正在执行的操作的更多详细信息,则可以提供更具体的答案。

Is the "function" something you could do in the database instead? It's really inefficient to process every row in the table to check for some type of condition. That's exactly what databases are good at, namely, processing queries efficiently and getting answers to you quickly.

So I'd recommend looking at how to do it all on the database side so that your PHP code is just fetching the end result (i.e. rows filtered by the function). Maybe if you provide more details of what your "function" is doing, a more specific answer can be provided.

薄暮涼年 2024-09-03 01:03:49

您可以使用 mysql_fetch_array,然后跳转到使用 $row[id] 而不是 $row['name'] 获取的值。
假设你的函数返回 true,你只需使用 $row[lastid+1]。

如果 ID 不是增量的,这可能会起作用:

$qry_result = mysql_query($qry) or die(mysql_error());
while ($row = mysql_fetch_array($qry_result)) {
    $result = yourfunction($row['whatever');
    if ($result != false)
         break;
}

还有一个 php 函数 next() 它将指针前进到下一个数组元素。在您的函数中,您可以实现一个数组生成器,然后用它在元素之间循环。取决于您的函数实际执行的操作或脚本的目的。如果有很多结果,可能会导致一些负载。

You can use mysql_fetch_array, and just jump on the values fetched using $row[id] and not $row['name'].
Say your function returns true, you'd just use $row[lastid+1].

If the ID isn`t incremental, this could work :

$qry_result = mysql_query($qry) or die(mysql_error());
while ($row = mysql_fetch_array($qry_result)) {
    $result = yourfunction($row['whatever');
    if ($result != false)
         break;
}

Also there is a php function next() which advances a pointer to the next array element. In your function you could implement an array builder, and then cycle between the elements with this. Depends on what your function actually does or script purpose is. It could lead to some load if there are alot of results.

桃酥萝莉 2024-09-03 01:03:49
  1. 如上所述,您不应该以这种方式检查数据库。数据库与纯文本文件有一点区别。
  2. 数据库中不应有一个字段包含由 value1:value2|value1:value2|value1:value2 分隔的一堆值。它必须是单独的字段。数据库和纯文本文件有一点区别,你最好学习一下。
  1. You should not check database this way, as mentioned above. Database has a little difference from the plain text file.
  2. You should not have a field in your database that has a bunch of values separated by value1:value2|value1:value2|value1:value2. It must be separate fields. Database has a little difference from the plain text file and you better learn it.
寻找一个思念的角度 2024-09-03 01:03:49

你可以尝试这样的事情:

SELECT *
FROM table
WHERE field NOT LIKE '%$sessionvar:%';

我想这就是你所追求的?

You could try something like this:

SELECT *
FROM table
WHERE field NOT LIKE '%$sessionvar:%';

I think that is what you are after?

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