SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    insert comma seperated array in mysql

    Hi, I am working on a script where I want to select parent id for a new user and save his parent id along with parents parent ids in mysql table.

    PHP Code:
    $last_id mysql_insert_id();

    //select parent id of last inserted user
    $sel mysql_query("SELECT * FROM tbl_new_agent WHERE new_agnt_id = '".$last_id."'");
    $row mysql_fetch_array($sel);
    $a $row['new_agnt_ParentId'];

    //select parents of parent which are supposed to be stored at parent_arr field in db as comma separated values                                     
    $sel1 mysql_query("SELECT * FROM tbl_new_agent WHERE new_agnt_id = '".$a."'");
    $row1 mysql_fetch_array($sel1);

    //here i want to add element in array.though its not working obviously
    $b array_push($row1['parent_arr'],$a);
                                         
    $update mysql_query("UPDATE tbl_new_agent SET parent_arr = '".$b."' WHERE new_agnt_id = '".$last_id."'"); 
    I have no idea how I can store comma separated values or store a single value initially and then add more elements.Can anyone give me idea in this regard?
    It's easy once you know how...

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Hi Anita,

    It seems like you are storing a tree structure of agents. That sounds a bit like a pyramid scheme. Are you running or working for someone who is running a pyramid scheme?

  3. #3
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply parallelist. Yes its almost like a pyramid scheme.I will use this data in another form where user selects an agent and by using in_array or something like that I will display text boxes in front of agent names.
    It's easy once you know how...

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume the field parent_arr is a varchar or text field in the DB?

    If so you would just append the new ID on as a string, or you could explode the current list and add it on and then implode it back.

    Code:
    $b = $row1['parent_arr'].','.$a;
    Or

    Code:
    $b = explode(',',$row1['parent_arr']);
    $b[] = $a;
    $b = implode(',',$b);
    Ben

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You never store comma separated arrays in a database - each entry in the comma separated array needs to be stored in its own row in a table in the database. This table would have a many to one relationship to the original table.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    You never store comma separated arrays in a database - each entry in the comma separated array needs to be stored in its own row in a table in the database. This table would have a many to one relationship to the original table.
    I agree normalization is the better solution. My response was based on what looked to be his decision to go the one field route for whatever reason.
    Ben

  7. #7
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much phpMyDirectory.this solution is much simpler during google search I also found that storing comma separated values is not a good idea.

    for now i have found this solution:
    Code:
    $update = mysql_query("UPDATE tbl_new_agent SET parent_arr = CONCAT('".$b."','".$a."',',') WHERE new_agnt_id = '".$last_id."'");
    and using in this way in another page:
    Code:
    $agent_id = $_POST['comm_Agent'];  
    $query = mysql_query("SELECT * FROM tbl_new_agent");
    
    while($row = mysql_fetch_array($query)){
             $parent_arr = explode(',',$row['parent_arr']);
                   if (in_array($agent_id, $parent_arr)) {
                                echo $row['new_agnt_id'];
                   }
    }
    i am sure there are other 'neat' ways to achieve this.but so far above code is working as expected. thanks for your replies felgall and phpMyDirectory.
    It's easy once you know how...

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    You're putting more work onto PHP than needs be.

    It "works" but it isn't the best way to go about it and you should really get away from both deprecated mysql_ and "storing what should be fields in a field" especially if you expect your database to get to any appreciable size.

    Can you ALTER your database structure?


Bookmarks

Posting Permissions

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