doujia1988 2015-07-31 12:14
浏览 107

将字符串转换为准备语句中的变量

I am creating a php script that imports a csv file into an existing mySQL database.

I am selecting the matching column headings by checking if the heading in in an array and trying to use the column number to create my input into my prepared statement.

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$numCols = count($data);
$row     = array();

// process the first row to select columns for extraction
if($isFirstRow) {
        $num = count($data);
        for($col=0; $col<count($columns); $col++){
            for($c=0; $c<$numCols; $c++)

                if(!in_array($data[$c], $columns[$col])){
                    if($c == ($numCols-1))
                        {
                            $matchingCol[$col] = '""';
                        }
                    }
                    else{
                        $matchingCol[$col] = 'data['.$c.']';
                         apc_store("foo$c", $matchingCol[$col]);
                }

        }

    $isFirstRow = false;
}

$data = array(
'contractorName' => (apc_fetch('foo1')) ,
'contractorType' => $matchingCol[3]); 
$query = "INSERT INTO uploadSQL SET" . bindFields($data);

$result = $pdo->prepare($query); 
$result->execute($data);

The data posted into the database is '$data[3]', '$data[5]' etc. How can I get the INSERT to input the data stored at $data[3] and not the string '$data[3]'?

  • 写回答

2条回答 默认 最新

  • dsriya5471 2015-07-31 12:48
    关注
    Use $data[$c] instead of 'data['.$c.']'...!?
    

    Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?