1

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).

2
  • Maybe you should consider making the restriction a part of your query. But it's hard to say with certainty, since you don't give any details or show any code. Commented Jun 22, 2011 at 11:28
  • i think you need to restrict you query only by putting WHERE condition i.e : WHERE product_category = 25 ..this would be batter solution.thx. Commented Jun 22, 2011 at 11:59

2 Answers 2

1

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 */ }
}
Sign up to request clarification or add additional context in comments.

3 Comments

you can fetch twice. You just have to reset it's internal pointer using mysql_data_seek()
Yea of course. But that wasn't his question or what he was trying (or should be trying) to do.
Ehhh.. sorry. Either you haven't understood the concept of retrieving rows, or your question didn't have much to do with what you were looking for. If you want information on ONE row, do NOT call mysql_fetch_array() twice! Fetch the data into an array (with mysql_fetch_array()) and go on from there.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.