有关wordpress wpdb类方法的一些整理

主要函数:

get_results

function get_results($query = null, $output = OBJECT)

返回SQL查询结果,返回值是一个由多行数据array组成的array,array(array(‘field1’=> ‘value1’, ‘field2’=> ‘value2’, …, ‘filedN’ => ‘valueN’));

例子:

foreach($db->get_results(‘select * from books’) as $book) {
	echo “name: $book->name<br>”;
	echo “author: $book->author<br>”;
}

get_row

function get_row($query = null, $output = OBJECT, $y = 0)

返回SQL查询的指定行内容

例子:

$book = $db->get_row(‘select * from books’, 3);
echo “name: $book->name<br>”;
echo “author: $book->author<br>”;

get_col

function get_col($query = null , $x = 0)

返回SQL查询的指定字段内容

例子:

foreach ($db->get_col(‘select * from books’, 1) as $author) {
	echo “author: $author”;
}

get_var

function get_var($query=null, $x = 0, $y = 0)

返回SQL查询的指定位置的值

例子:

foreach ($db->get_var(‘select * from books’, 2, 1) {
	echo “author: $author”;
}

insert

function insert($table, $data, $format = null)

向表中插入内容

例子:

$db->insert(‘books’, $array(‘name’=>’Breaking Bad’, ‘author’=>’David Lee’));

update

function update($table, $data, $where, $format = null, $where_format = null)

修改表中的数据

例子:

$db->update( ‘books’, array( ‘name’ => ‘Breaking Bad’), array( ‘id’ => 326 ));
原文地址:https://www.cnblogs.com/leavenup/p/2670443.html