1

I have a problem. I have a website with people and different transactions they make to and from a fake online bank. I want to be able to store an array of each person's transactions on my mysql database. I want each transaction to be defined as an associative array with a timestamp and the sql query that represents their transaction with the "bank".

Then I want those, after being serialized, to be the values of a transactions array that holds all of their transactions. Then I want to serialize that and store it in the database so that later I can add a transaction by unserializing it and appending a serialized array of another transaction to it. So far this code below works except that it just overwrites the one transaction and doesn't append a new one. I'd really appreciate any help.

Thanks in advance

function modify_transactions($row, $sql) { $sql=mysql_real_escape_string($sql); if(isset($row["TRANSACTIONS"])) { $transactions = unserialize($row["TRANSACTIONS"]); } else { $transactions = array(); } $transaction_array = array("timestamp"=>time(),"query"=>$sql); $transaction_data = serialize($transaction_array); $transactions[] = $transaction_data; $transactions_upload = serialize($transactions); $name = $row["NAME"]; $query = "UPDATE band.students SET TRANSACTIONS = '$transactions_upload' WHERE students.NAME = '$name'"; mysql_query($query); } 
3
  • If you var_dump($row), are you actually seeing $row['TRANSACTIONS'] having a string value? Commented Oct 22, 2011 at 0:14
  • yes I do it displays the serialized form of the latest transaction inside of transactions Commented Oct 22, 2011 at 0:25
  • Similar to: Storing arrays in MySQL? Commented Oct 22, 2011 at 16:15

2 Answers 2

3

If I were you, I'd rather go for a new table where every entry would represent a transaction and that had a foreign key student_id.

That'd be much, much, much cleaner and more flexible and scalable (i.e. what if you want to show the last 3 transactions of user X? What if a user had several million transactions?).

Sign up to request clarification or add additional context in comments.

Comments

1

First, you don't need to serialize each array, then serialize again. Serialize is recursive:

$array = array( array( '1', array() ), array( '2', array() ) ); $serialized = serialize($array); $unserialized = unserialize($serialized); echo "<pre>"; print_r($unserialized); echo "</pre>"; 

Prints:

Array ( [0] => Array ( [0] => 1 [1] => Array ( ) ) [1] => Array ( [0] => 2 [1] => Array ( ) ) ) 

So just serialize right before inserting into the database.

Second, you should change your database structure. Like vzwick mentioned, create a new table with a foreign key of the student. That way each entry represents a transaction.

Also, why are you storing the actual SQL query? That doesn't make any sense to me. Why don't you actually make a fake transaction?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.