I have another problem with my scripy now I have made it more advance, first off the count function doesnt work properly and it gives this error.
Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\social\add.php on line 42
Here is my script:
$query = mysql_query('SELECT friends FROM users WHERE id='$myid''); $friends = mysql_fetch_array($query); $friends2 = unserialize($friends['friends']); if (count($friends2) == 0) { //option 1 $friends2 = array($id); $friendsUpdated = serialize($friends2); mysql_query('UPDATE users SET friends='$friendsUpdated' WHERE id='$myid''); }else{ //option 2 array_push($friends2, $id); $friendsUpdated = serialize($friends2); mysql_query('UPDATE users SET friends='$friendsUpdated' WHERE id='$myid'');
If you run this code right after having created the database but before putting any data in there, ‘unserialize($friends[‘friends’]);’ returns something other than an array. Probably an empty string. So in option 2, you may want to do something like this before array_push:
This way, if the person has no friends by this point (sad.. but you’ll fix it by pushing a new friend to them), an empty friends list gets initialized.
Plus, any time you see two identical lines of code in different parts of the ‘if’ condition…
That’s a signal that you should restructure your code so that you’d only have one copy of these lines.