简体   繁体   中英

PHP how to check mysql result for values?

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 why 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 (eg 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM