开发者

PHP how to check mysql result for values?

开发者 https://www.devze.com 2023-03-14 11:31 出处:网络
I want to see whether there are rows with certain values in mysql result, for example whether any row in the result has product_category=25

I want to see whether there are rows with certain values in mysql result, for example whether any row in the result has product_category=25 Can I do that without using mysql_fetch_array and if I can't then wh开发者_运维技巧y can't I use mysql_fetch_array twice on the same result? Is there a workaround? Can I copy the result into another variable and run mysql_fetch_array on that? I tried that but the copy changes as the original changes (as mysql_fetch_array goes through it).


You can't use mysql_fetch_array twice (and get the same row), because it advances the pointer to the row in your result. (So if you retrieve multiple rows, the first call will fetch the first row, and the second will fetch the second row.)

As Shakti already pointed out, use a loop to go over all rows, and test for the expected value:

while ($row = mysql_fetch_array($result)) {
    if ($row['product_category'] == 25) { /* execute your code here */ }
}


Without knowing your code, I need to guess.

The mysql-result-resource is a cursor to the concrete result. This means, that you can only access the current entry directly. You must iterate over the result (e.g. via mysql_fetch_array()) to access every single result item. If you can't call it twice, you probably have just one single item as the result of your query, because you can call it as many times as you want, but if you don't have any more rows to retrieve from the resource, it will always return false.

0

精彩评论

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

关注公众号