最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dbdelta - Why my query 'REPLACE INTO...' does not work?

programmeradmin0浏览0评论

To insert or update data if data exists, I was using 'REPLACE INTO...' in a var $query_string like this :

dbDelta( $query_string ); 

For some reason, the row is not insert. Why ?

To insert or update data if data exists, I was using 'REPLACE INTO...' in a var $query_string like this :

dbDelta( $query_string ); 

For some reason, the row is not insert. Why ?

Share Improve this question asked Feb 8, 2021 at 14:10 J.BizMaiJ.BizMai 9002 gold badges10 silver badges30 bronze badges 1
  • dbDelta is extremely fussy over the query that it takes, down to the umber of spaces between sections of the query. Without seeing the full call with its value we can't answer this with 100% certainty, what is the full code that generates $query_string? What does the rest of the function look like? – Tom J Nowell Commented Feb 8, 2021 at 15:11
Add a comment  | 

1 Answer 1

Reset to default 2

The reason is due to this code inside dbDelta() :

// Create a tablename index for an array ($cqueries) of queries.
foreach ( $queries as $qry ) {
    if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
        $cqueries[ trim( $matches[1], '`' ) ] = $qry;
        $for_update[ $matches[1] ]            = 'Created table ' . $matches[1];
    } elseif ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) {
        array_unshift( $cqueries, $qry );
    } elseif ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) {
        $iqueries[] = $qry;
    } elseif ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) {
        $iqueries[] = $qry;
    } else {
        // Unrecognized query type.
    }
}

ths function dbDelta() does not support 'REPLACE INTO' and that calls in the else with // Unrecognized query type. comment.

发布评论

评论列表(0)

  1. 暂无评论