[web] Inserting array into mysql with php

Started by
7 comments, last by Sander 17 years, 8 months ago
Hi, Being rather new to php I can't find a way to insert array into php, later reading and modifying it. I tryed googeling it and didn't get any results (tutorials or code samples) for it. I'd be happy if someone could link me to one.
Advertisement
What do you mean by inserting an array in to php?

here is a list of array functions in php. maybe those will help you out ;-)

regards
thallish
regards/thallishI don't care if I'm known, I'd rather people know me
If you don't want to modify the array directly in the SQL database but only in PHP then you can use serialize().

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Thanx. I tryed to use serialize() and ended up with such code...what don't work :\
	$query = mysql_fetch_array ( mysql_query ( "SELECT Stocklist FROM users WHERE Username ='".$_SESSION['kasutaja']."'" ) );			if($query != 'puudub') { //puudub = default value  				$aktsiad = array ( unserialize ( $query ) );				if ( count ( $aktsiad ) < 11 ) {					array_push ( $aktsiad, $aktsia );					$aktsiad=serialize($aktsiad); 					$uuenda = mysql_query("Update users set Stocklist = '".$aktsiad."' where Username = '".$_SESSION['kasutaja']."'"); 					echo mysql_error();				} else {					echo 'Portfell on limiteeritud 10le aktsiale!';				}			} else {				$aktsiad = serialize ( $aktsia );				$uuenda = mysql_query("Update users set Stocklist = '".$aktsiad."' where Username = '".$_SESSION['kasutaja']."'"); 			}
From the code you posted, I can't really make up what you're trying to do.
But the principle is very simple.

Serializing an array and storing it in the database:
// create an array$array = array("dogbert234", "thallish", "sander");// insert the serialized array in the databasemysql_unbuffered_query("UPDATE Stocklist SET Stocklist = '" . serialize($array) . "' WHERE Username = '" . $_SESSION['kasutaja'] . "'"

Retrieving the array from the database and unserialize it:
// select the seralized array from the database$res = mysql_query("SELECT Stocklist FROM users WHERE Username = '" . $_SESSION['kasutaja'] . "'");$row = mysql_fetch_assoc($res);// unserialize the data$array = unserialize($row["Stocklist"]);//we now have the original array back
@Dogbert

Looking at your code, why not make StockList a separate table in the database? Something like:

tbl_Stocklist (int user_id, varchar aktsia)

And link the username and aktsia's through the user_id?

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

It's a stock portofilio management application and Stocklist is a list of stock symbols the user is monitoring. There is already a seperate table for stocks, so 3-table-link would be bit to complicated.

What I'm trying to do it:

First send a query to database to recieve the serialized array
If it's default value I just skip the reading part, serialize the users input and send it to database.
If there are already some symbols in the array I first unserialize the query to array, push the new value to it then serialize it again and set value of Stocklist to the one, where the input is already pushed and serialized.

It don't give me any error messages...it just don't work :\
Quote:What I'm trying to do it:

First send a query to database to recieve the serialized array
If it's default value I just skip the reading part, serialize the users input and send it to database.
If there are already some symbols in the array I first unserialize the query to array, push the new value to it then serialize it again and set value of Stocklist to the one, where the input is already pushed and serialized.


Sounds like you're making this more complicated than it needs to be. Why don't you store the information from the array into separate fields..?
Using a separate table makes it a lot easier. With MySQL 4.1 or higher you could do something like:

SELECT s.stock_name, s.stock_price FROM stock_table s WHERE  s.stock_id IN (    SELECT us.stock_id FROM user_stock_table us, user_table u WHERE      us.user_id = u.user_id      AND u.user_name = 'DogBert')


with tables like:

user_table (int user_id, varchar usernamae)stock_table (int stock_id, varchar stock_name, float stock_price)user_stock_table (int stock_id, int user_id)


And I would get the names and current price of all the stocks you are watching, with only one simple query.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

This topic is closed to new replies.

Advertisement