Jump to content


How To Store Selected Record In Variables Before Releasing

  • Please log in to reply
2 replies to this topic

#1 yshua

  • Members
  • PipPipPip
  • Advanced Member
  • 123 posts

Posted 28 December 2012 - 02:49 AM

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,


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,

#2 Jessica

  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 28 December 2012 - 02:50 AM

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.

Edited by Jessica, 28 December 2012 - 02:51 AM.

My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 DavidAM

  • Moderators
  • Advanced Member
  • 1,980 posts
  • LocationSpring, TX USA

Posted 28 December 2012 - 04:43 AM

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;
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);
} else {
  $foundRows = 0;

-- I haven't lost my mind, it's backed up on tape ... somewhere!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users