PHP 之将数组拼接为sql语句

一、代码

/**
 * 拼接sql语句
 * @param $table
 * @param $array
 * @return string
 */
function insertSql($table, $array)
{
    $sqlk = '';
    $sqlv = '';
    foreach ($array as $k => $v) {
        $sqlk .= ',' . $k;
        $sqlv .= ",'$v'";
    }
    $sqlk = substr($sqlk, 1);
    $sqlv = substr($sqlv, 1);
    $sql = "insert into $table ($sqlk) values ($sqlv)";
    return $sql;
}

 

posted @ 2022-11-17 13:21  样子2018  阅读(141)  评论(0编辑  收藏  举报