开发者

How to loop through array of database rows, and select the first with desired value?

开发者 https://www.devze.com 2022-12-18 08:02 出处:网络
I have an array of fields (not rows!) I want to loop through, in a table, in a predefined order, backwards (alternatively, I could reverse the order of the array) and return the field name of the firs

I have an array of fields (not rows!) I want to loop through, in a table, in a predefined order, backwards (alternatively, I could reverse the order of the array) and return the field name of the first field that returns a predefined value.

For example, the fields are like this:

field_a => 1
field_b => 0
field_c => 1
field_d => 0
field_e => 0

And say, field_c was the first with the value I was looking for, even though field_a may already possess the value.开发者_C百科 What is the best way to loop through e through a, and stopping at c when it's the first field with the value, and returning that field name?

I am working with PHP and MySQL, so I would prefer the returned field name to be a string, not an array - I don't need the value of the row();, only the field name itself.

Thank you! :)


Would this work?

SELECT *
FROM yourtable
WHERE criteria-for-property
ORDER BY whatever-ordering-you-want
LIMIT 1

This would retrieve 1 row and then stop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号