PHP 下一个 MySQL 行 - 如何移动指针直到函数检查为真?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在数据库中执行“函数”吗?处理表中的每一行来检查某种类型的条件确实效率很低。这正是数据库所擅长的,即高效处理查询并快速获得答案。
所以我建议看看如何在数据库端完成这一切,以便您的 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.
您可以使用 mysql_fetch_array,然后跳转到使用 $row[id] 而不是 $row['name'] 获取的值。
假设你的函数返回 true,你只需使用 $row[lastid+1]。
如果 ID 不是增量的,这可能会起作用:
还有一个 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 :
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.
你可以尝试这样的事情:
我想这就是你所追求的?
You could try something like this:
I think that is what you are after?