Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-12-2010, 12:41 PM   PM User | #1
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
Question PHP's serialize function not working!!

Hello all!
I have a script that SHOULD add friends to a users account by:
Checking the db to see if he has any friends.
If he doesn't, initialize an array with the friend he wants to add:
PHP Code:
array($friend); 
If he has friends already, unserialize his friends array
PHP Code:
$friends unserialize($row['friends']) 
and ARRAY_PUSH() the new friend on the end then serialize it:
PHP Code:
serialize(array_push($friends,$friend)) 
Then insert it into my DB.
What I don't get is the fact that in my db one time i get "a:1:{i:0;s:9:"some_user";}" next time i get "i:2;" then if I add another friend i get "b:0;"
HERE IS MY SCRIPT:
PHP Code:
<?php
session_start
();

if(
$_GET[cmd] == "friend"){

mysql_connect("localhost""user""pass") or die(mysql_error());
mysql_select_db("user_db") or die(mysql_error());

$friend $_GET['friend'];

$find_friends mysql_query("SELECT * FROM tablename WHERE user='$_SESSION[username]'");

while (
$row mysql_fetch_array$find_friends )) {
if(
$row['friends'] == "")
{
$new_friend_array = array($friend);
}
else
{
$new_friend_array array_push(unserialize($row['friends']),$friend);
}

print_r(unserialize($row['friends']));
print_r(array_push(unserialize($row['friends']),$friend));
}
$new_friend_array_serial serialize($new_friend_array);
echo 
$new_friend_array_serial;
print_r(unserialize($new_friend_array_serial));
mysql_query ("UPDATE tablename SET friends='$new_friend_array_serial' WHERE user='$_SESSION[username]'") or die (mysql_error());
header("Location: ../user/$_SESSION[username]&msg=$friend is now your friend!");
}
?>
This seems strange.. very strange. Hopefully somebody knows the answer to this question!!
My php version is: 5.2.8
Thanks,
Noisyscanner
noisyscanner is offline   Reply With Quote
Old 03-12-2010, 02:10 PM   PM User | #2
skywalker2208
Regular Coder

 
Join Date: Jan 2009
Posts: 193
Thanks: 0
Thanked 20 Times in 20 Posts
skywalker2208 is an unknown quantity at this point
So what exactly is the problem with the serialized string? If you unserialize the sting do you get the expected result?
skywalker2208 is offline   Reply With Quote
Old 03-12-2010, 03:19 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,657
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You're use of array_push is not correct. Array_push returns an integer result indicating the total number of items in the array. Attempting to serialize this will result in a serialized string of the datatype and count: {i:2} for example.
Correct this by splitting the array_push and serialize into two separate calls.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-12-2010, 09:54 PM   PM User | #4
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Correct this by splitting the array_push and serialize into two separate calls.
Great so how would i do that?
noisyscanner is offline   Reply With Quote
Old 03-12-2010, 10:11 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by noisyscanner View Post
Great so how would i do that?
Push the value into the array.

Then serialize it.
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 03-13-2010, 02:20 AM   PM User | #6
skywalker2208
Regular Coder

 
Join Date: Jan 2009
Posts: 193
Thanks: 0
Thanked 20 Times in 20 Posts
skywalker2208 is an unknown quantity at this point
Quote:
Originally Posted by noisyscanner View Post
Great so how would i do that?
Look at the example here.
skywalker2208 is offline   Reply With Quote
Old 03-14-2010, 08:40 PM   PM User | #7
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
Thanks!!!!
noisyscanner is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 PM.


Advertisement
Log in to turn off these ads.