0

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

2
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Will Gresham
0

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?

0

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

1

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);
Votes + Comments
Thankyou for the help, much appericated :)
0

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?

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.