Jump to content

How To Store Selected Record In Variables Before Releasing


yshua

Recommended Posts

Dear forum people:

 

Have PHP5.3.8, MySQL5.17, Apache2.2, Win 7. Am using too many languages at once. Still need to catch up learning how to store a record's fields into variables before using mysql_free_result():

 

$chk_txnid =  "SELECT 1, orderno, status FROM mytable WHERE txn_id='$tx_token'";
$result =  mysql_query($chk_txnid);
$num_rows = mysql_num_rows($result);

 

Later this is followed with,

 

mysql_free_result($result);

 

The system gives me the error to use mysql_free_result unless it appears before the update. Then the following code yields zero updates:

 

 $query = "UPDATE mytable SET status=1 , txn_id='$tx_token' where orderno ='$orderno'";

 

Can someone answer any of the two following questions:

 

How to store the read in record as variables, and

 

How to use a mysql_free_result() command AFTER the above update.

 

Grabbing at straws,

Yshua

The PHP Manual goes over how to use all the mysql functions, with detailed examples. Ignoring the fact that you should be using mysqli and you generally don't need to free the result, mysql_fetch_assoc should help.

The stated question is not very clear, and the provided snippets do not provide much insight. If you want to collect all of the data from the query ...

 

$data = array();  // Where we will store the data
$foundRows = 0;  // Number of rows returned
$sql = 'SELECT ...';
if ($res = mysql_query($sql)) {
 $foundRows = mysql_num_rows($res);
 while ($row = mysql_fetch_assoc($res)) {
   $data[] = $row;
 }
 mysql_free_result($res);
}

Although, doing $foundRows = count($data) after the loop will provide the same answer as mysql_num_rows() in this case.

 

I don't know what error you might be getting for not freeing the result before you do the another query (to update). If all you want is the number of rows, then don't select the data back, just select a count:

 

$sql = 'SELECT COUNT(*) FROM table WHERE condition = true';
if ($res = mysql_query($sql)) {
 $foundRows = mysql_result($res, 0, 0);
 mysql_free_result($res);
} else {
 $foundRows = 0;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.