将关联数组存储为单行以进行sql更新

时间:2013-03-28 13:00:50

标签: php

我想采用一个关联数组并解析它以放入SET查询的UPDATE部分:

我使用关联数组调用:

$this->ccQuery( 'emails', array('user_id' => $id, 'email_address' => $request->email) );

function ccQuery( $t, $a, $id=null )
{
        foreach($a as $key=>$value)
        {
            $setfields[] = $key . "='" . $value . "'";
        } 
        $setfields = implode(', ', $setfields);


        $testquery2 = 'UPDATE mytable_'.$t.' SET ' .$setfields. ' WHERE user_id="'.$user_id;
...
}

setfields为空。我做错了什么?

1 个答案:

答案 0 :(得分:0)

可能是这样的:

...

$testquery2 = "UPDATE mytable_"


foreach($a as $key=>$value)
        {
           $testquery2 = $testquery2 ."$t SET '$value' WHERE user_id= '$id'";
        } 

...