Array ( [0] => Array ( [sn] => 1 [mob_no] => 9602858989 [date] => 06-May-2015 [time] => 12:02:33 PM ) [1] => Array ( [sn] => 2 [mob_no] => 7795055128 [date] => 06-May-2015 [time] => 12:29:44 PM ) )

how to insert these array values in seperate database table fields?

Let's say, this array is called $myarray.

mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray["sn"], $myarray["mob_no"], $myarray["date"], $myarray["time"]));

thanks for reply Aeonix

my code is like this:

$output = file_get_contents($api_url);
if($output=="")
{
    echo "No output received";
}
else
{
    $arr_output = json_decode($output, true);
    if(isset($arr_output['msg']))
    {
        $msg = $arr_output['msg'];
        $msg_text = $arr_output['msg_text'];

        if($msg == "SUCCESS")
        {
            if(isset($arr_output['data']))
            {
                $arr_data = $arr_output['data'];

                // above array will contain your data
                // print_r($arr_data);
            }
            else
            {
                echo "Json data not set !!";
            }
        }
        else
        {   
            echo "Error : ".$msg_text;
        }
    }
    else
    {
        echo "Output not set !!";
    }
}

while($item = array_shift($arr_data))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

right now i am getting output as

sn => 1 mob_no => 9602858989 date => 06-May-2015 time => 12:02:33 PM sn => 2 mob_no => 7795055128 date => 06-May-2015 time => 12:29:44 PM

now how to store into database table fields like sn, mob_no, date, time

$output = file_get_contents($api_url);
if($output=="")
{
    echo "No output received";
}
else
{
    $arr_output = json_decode($output, true);
    if(isset($arr_output['msg']))
    {
        $msg = $arr_output['msg'];
        $msg_text = $arr_output['msg_text'];
        if($msg == "SUCCESS")
        {
            if(isset($arr_output['data']))
            {
                $arr_data = $arr_output['data'];
                // above array will contain your data
                // print_r($arr_data);

                mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray["sn"], $myarray["mob_no"], $myarray["date"], $myarray["time"]));
            }
            else
            {
                echo "Json data not set !!";
            }
        }
        else
        {   
            echo "Error : ".$msg_text;
        }
    }
    else
    {
        echo "Output not set !!";
    }
}
while($item = array_shift($arr_data))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

I'm assuming that commented array is my target array. You need to make sure to have config file out there, and include it, which would connect to SQL database. And execute the script.

If it's still not working, specify where exactly are you getting your input from.

@Aeonix, your query is having problem with the double quotes. mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray["sn"], $myarray["mob_no"], $myarray["date"], $myarray["time"])); The sn,mob_no,date,time will read as variable and is not defined.

commented: Thanks for the information, I really forgot and haven't even noticed. +2

Oh, crap! Thanks! Oh, crap, use this one instead:

$output = file_get_contents($api_url);
if($output=="")
{
    echo "No output received";
}
else
{
    $arr_output = json_decode($output, true);
    if(isset($arr_output['msg']))
    {
        $msg = $arr_output['msg'];
        $msg_text = $arr_output['msg_text'];
        if($msg == "SUCCESS")
        {
            if(isset($arr_output['data']))
            {
                $arr_data = $arr_output['data'];
                // above array will contain your data
                // print_r($arr_data);
                mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray['sn'], $myarray['mob_no'], $myarray['date'], $myarray['time'])");
            }
            else
            {
                echo "Json data not set !!";
            }
        }
        else
        {   
            echo "Error : ".$msg_text;
        }
    }
    else
    {
        echo "Output not set !!";
    }
}
while($item = array_shift($arr_data))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

As from my view, since $arr_data containing values of Array ( [0] => Array ( [sn] => 1 [mob_no] => 9602858989 [date] => 06-May-2015 [time] => 12:02:33 PM ) [1] => Array ( [sn] => 2 [mob_no] => 7795055128 [date] => 06-May-2015 [time] => 12:29:44 PM ) )
So probably we need a looping and since the query cannot directly nested an array variable, I suggested:

foreach($arr_data as $data){
    $query = "INSERT INTO table (sn, mob_no, date, time) VALUES (".$data['sn'].", ".$data['mob_no'].", ".$data['date'].", ".$data['time'].")";
    //carry out the query here
}
commented: True, haven't thought of that. +0

Okay, forget what I said, ^^^^, better solution.

Is it me or are we open to SQL injections here as we're using raw input and not sanitizing. Better to use prepared statements.

We provide answer to unfinished script, hoping, that the OP will change his/her script sometime to more secure one. As of now, the question is other than "how do I protect my script?".

Member Avatar for diafol

We provide answer to unfinished script, hoping, that the OP will change his/her script sometime to more secure one. As of now, the question is other than "how do I protect my script?".

I disagree as I don't think this can be assumed. If you ('the expert') provide solutions using an unsecure method for the 'beginner', then I would assume that the beginner would accept the solution as being safe to use. If you have some experience with a language and can see the dangers, e.g. SQL injections, I think you are duty-bound to point this out to a user.

thanks lps, Aeonix & diafol for helping me to solve this issue.

one more thing, how to check the mob_no is already exist in table, it should store unique values i.e. unique mobile number in mob_no field

Member Avatar for diafol

Ensure that the mob_no field is indexed (unique):

CREATE UNIQUE INDEX indexname ON tablename (fieldname)

Give indexname a memorable 'short' name

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.