Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    So what exactly is the problem with the serialized string? If you unserialize the sting do you get the expected result?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by noisyscanner View Post
    Great so how would i do that?
    Push the value into the array.

    Then serialize it.

  • #6
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by noisyscanner View Post
    Great so how would i do that?
    Look at the example here.

  • #7
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks!!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •