duanjia2415 2017-11-01 15:57
浏览 59
已采纳

从解析数据中将数组插入MySQL

I have a script that parses data from HTML tables and puts it into an array. Here's the sample output from the script:

Array
(
    [] => Array
        (
            [] => Array
                (
                    [] => Array
                        (
                            [] => Array
                                (
                                    [] => 1
                                )
                        )
                )
        )

    [Precinct Code] => Array
        (
            [Precinct] => Array
                (
                    [Total Registered] => Array
                        (
                            [Total Voting] => Array
                                (
                                    [Percent Voting] => 1
                                )
                        )
                )
        )

    [004] => Array
        (
            [AWENDAW] => Array
                (
                    [1299] => Array
                        (
                            [926] => Array
                                (
                                    [71.285] => 1
                                )
                        )
                )
        )
)

I want to insert these arrays into a MySQL database, and I am using the following code:

    $html = file_get_html('URL GOES HERE');
foreach($html->find('tr') as $row) {
    $precinct = $row->find('td',0)->plaintext;
    $precinctCode = $row->find('td',1)->plaintext;
    $totalRegistered = $row->find('td',2)->plaintext;
    $totalVoting = $row->find('td',3)->plaintext;
    $percentVoting = $row->find('td',4)->plaintext;

    $table[$precinctCode][$precinct][$totalRegistered][$totalVoting][$percentVoting] = true;

    /* Dump each array into MySQL table */
    $db = new PDO('mysql:host=localhost;dbname=MY_DATABASE_NAME', 'USERNAME', 'PASSWORD');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $insert = $db->prepare(
        'INSERT INTO rsltStats (precinctCode , precinct, totalRegistered,   
      totalVoting, percentVoting ) VALUES (?, ?, ?, ?, ?)');

    foreach($table as $values) {
        $insert->execute($values);
    }
}

However, it keeps throwing an error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens'

I am fairly new to this so any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dongshou7903 2017-11-01 16:06
    关注

    Instead of doing this:

    $table[$precinctCode][$precinct][$totalRegistered][$totalVoting][$percentVoting] = true;
    ...
    foreach($table as $values) {
        $insert->execute($values);
    }
    

    This is looping on $table, which only has one row where the index is precinctCode, and then $values ends up with only 4 elements. Not 5.

    Try instead simply this:

    $values = array($precinctCode,$precinct,$totalRegistered,$totalVoting,$percentVoting);
    ...
    $insert->execute($values);
    

    IF you are needing that $table array afterwards, you can still build that $table[][][] nested array like you are, but dont use it to do the sql.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号