Jump to content
  • Advertisement
Sign in to follow this  
dogbert234

[web] Inserting array into mysql with php

This topic is 4461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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']."'");
}

Share this post


Link to post
Share on other sites
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 database
mysql_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

Share this post


Link to post
Share on other sites
@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?

Share this post


Link to post
Share on other sites
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 :\

Share this post


Link to post
Share on other sites
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..?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!