When WordPress writes Plug-ins, it discovers the need to call the database
Wpdb no matter in front of the template, or in the background of plug-ins, are free to use
When using WordPress, if you want to directly use WP's encapsulated database operations Class (wp-db.php), the wp-blog-header.php included in the code can be used.
Define (' PATH ', DirName (dirname (__file__)). ' /’);
Require_once (PATH. ‘.. /wp-blog-header.php ');
Global $wpdb;
One of the ways to insert data is to use the Insert () function in the Wp-db class.
$table = "test_table";
$data _array = Array (
' Column_1′=> ' data1′,
' Column_2′=> ' data2′
);
$wpdb->insert ($table, $data _array);
The first parameter is the name in the database table, the second parameter is the data to insert, and an array. The name of the key in the array is the column name in the table. In fact, the Insert () function also has a third parameter format, which can be used by the update () function when interested friends can look at updating data in the Wp-db.php method definition, for example:
$table = "test_table";
$data _array = Array (
' Column_1′=> ' new_data1′
);
$where _clause = Array (
' Column_2′=> ' data2′
);
$wpdb->update ($table, $data _array, $where _clause);
There are a number of ways to get data from a database, one of the following:
$querystr = "Select column_1 from test_table";
$results = $wpdb->get_results ($QUERYSTR);
$i = 0;
while ($i < count ($results)) {
echo $results [$i]->column_1.] <br/> ";
$i + +;
}
Inquire
<?php $wpdb->query ("DELETE from $wpdb->post WHERE post_id = ' 13′");?>
Where the query parameter is any MySQL statement. The return value is how many rows are selected and affected. Returns False if an error occurs.
Select a variable
<?php $wpdb->get_var (' query ', column_offset,row_offset);?>
Where query is the MySQL statement to query, if it is empty, it is selected from the cache. Column_offset and Row_offet represent the first columns and rows of query return values, with a default value of zero. Typical uses are:
<?php $user _count = $wpdb->get_var ($wpdb->prepare ("SELECT count (*) from $wpdb->users;")); >
This SQL selects only one value, the default 0 rows 0 columns, which indicates the number of users selected. It is not clear why this is always added to the prepare.
Select a line
<?php $wpdb->get_row (' query ', Output_type, row_offset);?>
Query for the MySQL statement to execute, Output_type indicates that the return value is Object,hash or array; Row_offset represents the first few lines.
By default, Output_type is object.
$mylink = $wpdb->get_row ("select * from $wpdb->links WHERE link_id = 10");
Echo $mylink->link_id; Prints "10"
If output_type=array_a, then:
$mylink = $wpdb->get_row ("select * from $wpdb->links WHERE link_id = ten", array_a);
echo $mylink [' link_id ']; Prints "10"
If Output_type=array_n, then:
$mylink = $wpdb->get_row ("select * from $wpdb->links WHERE link_id = ten", array_n);
echo $mylink [1]; Prints "10"
Select a column
<?php $wpdb->get_col (' query ', column_offset);?>
General Election
Java Code Collection Code
$wpdb->get_results (' query ', output_type);
<?php
$fivesdrafts = $wpdb->get_results ("Select ID, post_title from $wpdb->posts
WHERE post_status = ' draft ' and Post_author = 5 ');
foreach ($fivesdrafts as $fivesdraft) {
Echo $fivesdraft->post_title;
}
Insert a row
<?php $wpdb->insert ($table, $data, $format);?>
<?php $wpdb->insert (' table ', Array (' Column1 ' => ' value1 ', ' Column2 ' => 123), Array ('%s ', '%d '))?>
Update
$wpdb->update ($table, $data, $where, $format = null, $where _format = null);
<?php $wpdb->update (' table ', Array (' Column1 ' => ' value1 ', ' column2 ' => ' value2 '), Array (' ID ' => 1), AR Ray ('%s ', '%d '), array ('%d '))?>
About Wpdb Prepare
It is not clear why each MySQL statement is wrapped in prepare, as explained here: because a MySQL statement may contain characters such as single quotes and double quotes, sending MySQL directly without processing can cause errors. So here, a prepare is used to preprocess the MySQL statement. The syntax for prepare is:
$sql = $wpdb->prepare (' query ' [, Value_parameter, Value_parameter ...]);
You can include%d,%s,%f in query, which means that the type of the following argument is an integer, a character, and a floating-point, and if you want to display the%, then the%, syntax, and the C language are basically the same as printf.
We're almost done here. There should be no problem with the processing of the general database. If you encounter a problem, you can refer to it at the beginning of this article.
$WPDB is a global variable that contains multiple query functions about the database:
$wpdb-> get_results (' query ');
$wpdb->query (' query ');
$wpdb->get_var (' query '), Column_offset,row_offset);
$wpdb->get_row (' query ', Output_type, Row_offset);
$wpdb->get_ Col (' query ', column_offset);
$wpdb->get_results (' query ', output_type);
$wpdb->insert ($ Table, $data, $format);
$wpdb->update ($table, $data, $where, $format = null, $where _format = null);
$wpdb->prepare (' query ' [, Value_parameter, Value_parameter ...]);
$wpdb->show_errors ();
$wpdb->hide_errors ();
$wpdb->print_error ();
$wpdb->get_col_info (' type ', offset);
$wpdb->flush ();