A quick quesiton (cant seem to get what I want on google - prolly not looking in the right places).

I remember using a function that allowed you to reuse a query if it has already been used.
I thought it was mysql_free_result($query) and it would let you use the $query again which is $query = mysql_query("select * from table").

But it is not working at the moment :(

What was the correct function?

Thanks, Regards X

Recommended Answers

All 5 Replies

It is the mysql_free_result function you would use, example below:

$connect = mysql_connect("server", "username", "password");
if (!$connect) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbName",$connect);

$sqlQuery = "SELECT * from table";
$result = mysql_query($sqlQuery,$connect);
// Do something with the result

// Free result
mysql_free_result($result);

What error (if any) does the script return?

It is the mysql_free_result function you would use, example below:

$connect = mysql_connect("server", "username", "password");
if (!$connect) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbName",$connect);

$sqlQuery = "SELECT * from table";
$result = mysql_query($sqlQuery,$connect);
// Do something with the result - first instance

// Free result
mysql_free_result($result);

// Then i would have the line here, to be run again - second instance
$result = mysql_query($sqlQuery,$connect);

What error (if any) does the script return?

There is no error as such just that the data is only displayed in the first instance and not in the second

try doing this:

// Then i would have the line here, to be run again - second instance
$result = mysql_query($sqlQuery,$connect);
if(mysql_num_rows($result)>0) {
//Process second instance
} else {
echo 'No Data';
}

This will tell you if the query is actually fetching a row, if it is, make sure that you put the result into an array before trying to use it:

$data = mysql_fetch_array($result);
commented: Thankyou for the help, much appericated :) +1

I have ended up just renaming the array to avoid the error and differitiate between the two instances but I might in the near future need to reuse it. So you think for the second instance I should just rerun the query and it will work for future use?

If you are using the same name for the array in both instances, try unsetting it first:

$arrayName='';

Once you have executed a second query you then need to reload the data from the query into the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.