WordPress全域資料庫物件$wpdb – 新增、更新、刪除
閱讀本文前,請先閱讀:WordPress全域資料庫物件$wpdb – 資料查詢
在上面的文章裏,我們學會了基本的資料查詢(SELECT),也就是CRUD操作的Read功能。
// Select/Read function myFunction() { global $wpdb; $wpdb->get_var("Query"); $wpdb->get_row("Query", Format_parameter); $wpdb->get_col("Query"); $wpdb->get_results("Query", Format_parameter); }
Create/Insert
類似SQL的insert 語法:INSERT INTO table_name (cols) VALUES (vals)global $wpdb; $wpdb->insert( $table, $data, $format );例:
global $wpdb; $wpdb->insert($wpdb->prefix."posts", array( "post_title" => "Sample Post", "post_content" => "This is sample content of this post", "post_name" => "sample-post", "post_status" => "publish" ),array( "%s", "%s", "%s", "%s" )); // SQL Insert寫法: // INSERT INTO wp_posts ("post_title", "post_content", "post_name", "post_status") // VALUES ("Sample Post", "This is sample content of this post", "sample-post", "publish");第三個參數$format非必要,若給的話,以上的例子來說%s是指字串,其他還有:
- %d – Integer Value
- %f – Float Value etc.
global $wpdb; $wpdb->insert($wpdb->prefix."posts", array( "post_title" => "Sample Post", "post_content" => "This is sample content of this post", "post_name" => "sample-post", "post_status" => "publish" ));當$wpdb->insert執行完畢之後,可以透過$wpdb->insert_id取得自動增號的ID值。
global $wpdb; $row_id = $wpdb->insert($wpdb->prefix."posts", array( "post_title" => "Sample Post", "post_content" => "This is sample content of this post", "post_name" => "sample-post", "post_status" => "publish" )); echo $wpdb->insert_id; // It prints the return row ID after insertion.除了使用$wpdb->insert方法,我們也可以用query字串的方式 (通用的SQL敘述)做到一樣的功能:
global $wpdb; $query = $wpdb->prepare('INSERT INTO wp_posts ("post_title", "post_content", "post_name", "post_status") VALUES ("Sample Post", "This is sample content of this post", "sample-post", "publish")'); $data = $wpdb->query($query); echo $wpdb->insert_id;
Update
語法global $wpdb; $wpdb->update($table, $data, $where, $format = null, $where_format = null);
參數解釋
- $table – 資料表格
- $data – 要更新的資料
- $where – 條件式
- $format – 資料的格式字串 %s:字串/string, %d:整數/integer, %f:浮點數/float
- $where_format – Where字句的格式字串
global $wpdb; $wpdb->update( $wpdb->prefix."posts", array( "post_title" => "Updated Post Title", "post_content" => "This is sample content update of this post", "post_name" => "my-updated-post", "post_excerpt" => "Sample content update" ), array("ID" => 4), array("%s", "%s", "%s","%s"), array("%d") );可以用SQL查詢字串的方式來做到上面的功能: 語法
global $wpdb; $wpdb->prepare("UPDATE table_name SET COLS = VALS WHERE CONDITIONS");範例:
global $wpdb; $sql = 'UPDATE wp_posts SET post_title = "Updated Post title", post_content = "Updated Sample content", post_name = "updated-post-name" WHERE ID = 4'; $prepared_query = $wpdb->preapre($query); $data = $wpdb->query($prepared_query);
Delete
語法global $wpdb; $wpdb->delete(string $table, array $where, array|string $where_format = null)範例
global $wpdb; $wpdb->delete( $wpdb->prefix."posts", array("ID" => 4), array("%d") );用SQL查詢字串的方式來做到上面的功能:
global $wpdb $query = "DELETE FROM wp_posts WHERE ID = 4"; $prepared_query = $wpdb->preapre($query); $data = $wpdb->query($prepared_query);