Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a list of contact for friends in a database, what I am trying to do is retrieve a users contacts, see if the other user id is in there, if it is not then add it then put it back in the database.

however I seem to keep adding the same contact into the array even when it is already thereand the output is stored in my db like this

a:2:{i:0;a:1:{i:0;i:1070;}i:1;i:1070;}

This is the product of the in_array not evaluating as true on the first check. Every time time the function is run more and more "layers" are added.

Thanks in advance

here is my code

function addfarmertohunter($hunterid,$farmerid){
    $select="SELECT contacts from users where id=$hunterid";
    $result=mysql_query($select)or die(' add farmer error'. mysql_error());
        if (mysql_num_rows($result)==1){
        $row=mysql_fetch_array($result);
        $contacts=$row[0];  
        if($contacts==null||$contacts=="N;"||$contacts==""){
            $temp=array();
                        array_push($temp, $farmerid);
        }else{
            $temp=array(unserialize($contacts));
            if(in_array((int)$farmerid, $temp)==FALSE){array_push($temp, $farmerid);}
        }
        $strcontacts=serialize($temp);
        $ins="UPDATE users set contacts='$strcontacts' where id=$hunterid";
        mysql_query($ins) or die('add farmer e'.mysql_error());
        }
}
share|improve this question

closed as too localized by Ja͢ck, Second Rikudo, hjpotter92, Soner Gönül, gcbenison Jun 11 '13 at 6:23

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Why are you storing like that? Why not have a table with each contact in a row? –  Barmar Jun 11 '13 at 2:54
    
@Barmar what as in cols Farmerid. Hunterid, row1 1,2 row2,1,14? would this be more efficient? –  Mark Gilchrist Jun 11 '13 at 18:36
    
Yes, that's the preferred way to do it. Look up "relation table" and "many-to-many". –  Barmar Jun 11 '13 at 18:45
    
may be that would be a lot better, I have a feeling my current approach reflects that I have been writing php for 6 weeks, thanks for all your comments, they have been a real help –  Mark Gilchrist Jun 11 '13 at 18:58
    
It's not a PHP issue, it's database design. Did you learn about "normalization" when studying databases (did you "learn" any of this stuff, or are you just picking it up as you go along)? –  Barmar Jun 11 '13 at 19:07

2 Answers 2

up vote 2 down vote accepted

First change this

$temp = array(unserialize($contacts));

$temp = unserialize($contacts);

and don't check with == or ==== in in_array try like this

if(in_array((int)$farmerid, $temp)){array_push($temp, $farmerid);}

OR

if(!in_array((int)$farmerid, $temp)){array_push($temp, $farmerid);}

Because returns TRUE if needle is found in the array, FALSE otherwise.

share|improve this answer
    
thanks that has fixed it –  Mark Gilchrist Jun 11 '13 at 3:10

Change:

$temp = array(unserialize($contacts));

to:

$temp = unserialize($contacts);

unserialize() returns the array, so you're wrapping an extra level of array around it each time.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.