Merge 3 arrays to insert in one row in db

Discussion in 'PHP' started by ausgezeichnete, May 7, 2009.

  1. #1
    i have those three arrays for example
    $num=array(1,2,3);
    $name=array('one','two','three');
    $letter=array('m','k','n');

    what i want is to loop through all of them and insert them like that:

    insert into mydb (field1,field2.field3) values(1,'one','m');
    etc..
     
    ausgezeichnete, May 7, 2009 IP
  2. NicheMate

    NicheMate Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This should get you started on the right track.

    
    $num=array(1,2,3);
    $name=array('one','two','three');
    $letter=array('m','k','n');
    
    
    while ($value = current($num)) {
        $pos = key($num);
    	$sql = "insert into mydb (field1,field2.field3) values($value,'$name[$pos]','$letter[$pos]')";
    	echo $sql; // Action the sql here using mysql_query($sql) or whatever.
        next($num);
    }
    PHP:
     
    NicheMate, May 7, 2009 IP
  3. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah my Dear you can do this in php

    Let's look how to do this.

    $i=0;

    while($i<3)
    {
    $query="insert into yourtable values('$num[$i]','$name[$i]','$letter[$i]')";
    $result=mysql_query($query);
    $i=$i+1;
    }

    if($result)
    {
    echo "Records Successfully Entered";
    }
    else{
    echo mysql_error();
    }

    This code will do what you want.

    Happy Coding...!
     
    mrmaf, May 7, 2009 IP