循环中PDO内的mysql查询

时间:2017-02-13 14:30:22

标签: php mysql pdo

基本上,我有一个PDO形式的老开发人员,我需要在其中运行一个开关语句来交换值,但我的循环每次都失败,我已经尝试了标准的mysqli查询,获取数组等等它没有工作..我是PDO的新手,我是如何让查询运行或构建在switch语句中来比较值的?

private function loadNineFixedData( $filepath, $table ){

    //We are going to insert some data into the users table
    $sth = $this->dbcon->prepare(
    "INSERT INTO " . $table . "(cli, date, time, destination, dialled, duration, cost, destcode, extention, product, rate) 
                            VALUES (:cli, :date, :time, :destination, :dialled, :duration, :cost, :destcode, :extention, :product, :rate)"
    );
    $dest = $this->$dbcon->prepare("SELECT * FROM calltypes_nine where name = ? ");
    $dest->bindParam(":name", $description);
    $csv = \League\Csv\Reader::createFromPath( $filepath );



//now back to the csv reader to kick us off
$nbInsert = $csv->each(function ($row) use (&$sth) {

            $description = $row[11];

            $dest->execute();
            while($codes = $dest->fetch(PDO::FETCH_BOTH)){
                $code = $codes['destination'];
            }

        $cli = '0'.substr($row[2], 3);
        $time = str_replace(':','', $row[6]);
        $dialled = '0'.substr($row[3], 3);

             switch($row[11]){
                    case 'Standard':
                        $rate = 'STD';
                        break;
                    case 'Economy';
                        $rate = 'EVE';
                        break;
                    case 'Weekend':
                        $rate = 'WKD';
                        break;
              }
              switch ($row[1]) {
                    case 'N':
                        $prod = 'IICS';
                        break;

                    default:
                        $prod = 'VOCE';
                        break;

    //Do not forget to validate your data before inserting it in your database
    $sth->bindValue(':cli', $cli, \PDO::PARAM_STR);
    $sth->bindValue(':date', $row[4], \PDO::PARAM_STR);
    $sth->bindValue(':time', $time, \PDO::PARAM_STR);
    $sth->bindValue(':destination', $row[9], \PDO::PARAM_STR);
    $sth->bindValue(':dialled', $dialled, \PDO::PARAM_STR);
    $sth->bindValue(':duration', $row[6], \PDO::PARAM_STR);
    $sth->bindValue(':cost', $row[12], \PDO::PARAM_STR);
    $sth->bindValue(':destcode', $code, \PDO::PARAM_STR);
    $sth->bindValue(':extention', $row[14], \PDO::PARAM_STR);
    $sth->bindValue(':product', $prod, \PDO::PARAM_STR);
    $sth->bindValue(':rate', $rate, \PDO::PARAM_STR);

    return $sth->execute(); //if the function return false then the iteration will stop
    });        

 }

0 个答案:

没有答案