[PHP OOP/MySQL] Friends Add/View system
Well, today I worked on a small friends system for my housekeeping. At the moment I only have add a new friend, and view your old friend but I'm aiming to add more soon.
I wondered if there was a cleaner way to do this, or if my codes are okay as they stand.
PHP Code:
/**
*Grab users friends
*/
public function getFriends($user) {
global $db;
$qry = $db->query("SELECT * FROM `users` WHERE username='$user'");
$ass = $db->ass($qry);
$friends = explode(',', $ass['friends']);
echo $user ."'s Current Friends:<br/>";
foreach($friends as $x) {
echo $x .'<br />';
}
}
/**
*Add friend function
*/
public function addFriend($user, $friend) {
global $db;
$qry = $db->query("SELECT * FROM `users` WHERE `username`='$user'");
$ass = $db->ass($qry);
$addFriend = $ass['friends'].",".$friend;
$db->query("UPDATE `users` SET `friends`='$addFriend'");
}
Re: [PHP OOP/MySQL] Friends Add/View system
You might want to add a check whether a friend with that name already exists?
Also, I presume that your 'ass' function returns something along the lines of a mysql_fetch_array?
Btw, you need to change the query in addFriend. Now youre setting the friends for every entry in `users`! Add a "WHERE `username`='$user'" to it.
Re: [PHP OOP/MySQL] Friends Add/View system
I'll be adding that soon - I just made the script quickly today, I need to make it more advance after I've finished some work.
Ah yeah, ass = assoc, arr = array. The script is now using array.
And oh yeah, thanks for picking that up! I'll add that now :)