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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.