如何从WordPress的dbDelta()函数中获取受影响的行数

时间:2017-03-19 作者:Shapon Pal

我正在使用dbDalta导入数据库中的这么多行。现在,我想检查所有行插入是否成功,受影响的行数和导入失败的行数。

我的代码:

$beginQuery = "INSERT INTO ".$wpdb->prefix."table_name(pzipcode, zip_class, pcity, pstate, lattitude, longitude, pcountry, pstatus, ptime, id) VALUES ";

$zips  = array(
  array(\'pzipcode\' => \'AB10\',\'latitude\' => \'57.13514\',\'longitude\' => \'-2.11731\',\'pcity\' => \'Aberdeen\',\'pstate\' => \'Scotland\',\'pcountry\' => \'SCT\'),
  array(\'pzipcode\' => \'AB11\',\'latitude\' => \'57.13875\',\'longitude\' => \'-2.09089\',\'pcity\' => \'Aberdeen\',\'pstate\' => \'Scotland\',\'pcountry\' => \'SCT\'),
// ...................................
// ...................................
);

foreach($zips as $zip) {
     //$beginQuery.\' (\'.$zip[\'zp\'].\',"\'.$zip[\'ct\'].\'","\'.$zip[\'st\'].\'","United States",1,"\'.current_time(\'mysql\', 1).\'")\';
    $zipCodeDumper = dbDelta($beginQuery.\' ("\'.$zip[\'pzipcode\'].\'","\'.$zip[\'zip_class\'].\'","\'.$zip[\'pcity\'].\'","\'.$zip[\'pstate\'].\'","\'.$zip[\'lattitude\'].\'","\'.$zip[\'longitude\'].\'","\'.$zip[\'pcountry\'].\'","\'.$zip[\'pstatus\'].\'","\'.$zip[\'ptime\'].\'","\'.$zip[\'id\'].\'")\');
    //break;
  }
  if (!$zipCodeDumper) {
    echo "Successfully Imported all Zipcode/postcode.";
  }else{
    echo "Something was wrong!!! Please Try again";
  }

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

dbDelta() 用于创建新表或更改现有表的结构(而不是数据)。

你需要$wpdb->insert( $tablename, $zips ); 当您要插入数据时。因此得名。:)

此方法返回受影响的行数,与您的需要完全一致。如果它返回false, 看看$wpdb->last_error 看看到底出了什么问题。

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请