0

I'm trying to reset the data in my variable so I can call from it again. If there is data in the first query, I need it to run the reset. First I'm checking for data, and second, if there is data, I'm setting the index back to 0.

This works in my localhost environment, but not in my live environment. I know I'm getting the error I am getting because the first query isn't turning up any results, so mysql_num_rows($query) is empty.

I get the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource.

(This code works when the first query turns up results, successfully resetting and displaying the second query.)

if (mysql_num_rows($query)){
    mysql_data_seek($query, 0);
}

Is there a better way I can check if the first query has information, without spitting out an error if there isn't any information found?

5
  • Any reason you need to rewind the results? Not all versions/configurations of the php mysql driver support this. A better method is to fetch/save the results into a php data structure using a single pass through the results, then using that data structure anywhere you need to use/reuse the data. Commented Sep 11, 2012 at 21:20
  • 1
    I feel like there's some error in the query, to be honest. I've used num_rows to return 0 before. Commented Sep 11, 2012 at 21:22
  • Please don't use the mysql_ functions, use PDO or atleast MySQLi...you're just asking for trouble.. Commented Sep 11, 2012 at 21:23
  • As Eric wrote, mysql_num_rows returns 0 when the query doesn't return any row. Commented Sep 11, 2012 at 21:24
  • off topic but important: Please note that the mysql_xxx() functions are considered obsolete. The PHP manual has a notice on all relevant pages strongly recommending switching to either the equivalent mysqli_xx() functions or the PDO library instead. The newer libraries are more modern, better functionality, and most importantly, better security if you use the prepared statement feature. Commented Sep 11, 2012 at 21:24

1 Answer 1

1

note the other comments regarding using mysql_? functions.

if you are going to be iterating through the results more than once for some reason, then dump them into an array in your first pass as Marc B commented. For example (still using your mysql_? functions);

$result = mysql_query($query);
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
while($row = mysql_fetch_row($result)){
    $myResultSet[]=$row;
}

once done, we can iterate through that set as often as we like

foreach($myResultSet as $row){  # note foreach resets each time
    # do stuff with $row['fieldname']
}

You should take this opportunity however to update your mysql_ to more up to date methods... this underlying solution would remain the same.

If that doesn't answer it, post more code so we can understand why and what you are trying to do.

Sign up to request clarification or add additional context in comments.

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.