WordPress函数中mysql\\u free\\u result和mysql\\u ping的替代函数是什么,WordPress函数是否提供了php等所有函数?是否有必要为大型WordPress数据库请求释放结果内存?
1 个回复
最合适的回答,由SO网友:birgire 整理而成
您有以下方法$wpdb
对象:
$wpdb->flush()
包含对的调用mysqli_free_result()
或mysql_free_result()
如果不支持。$wpdb->check_connection()
包含对的调用mysqli_ping()
或mysql_ping()
如果不支持。当然,您可以在WordPress中使用所有PHP函数。
下面是一个基于\\wpdb
类本身:
// Use the global instance created by WordPress
global $wpdb;
// Fetch our data with some huge query:
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->posts}" );
// ... some data handling here
// Let\'s flush for another huge query.
// But that\'s not actually needed,
// since this is already done in the $wpdb->query() call
// that\'s used within the $wpdb->get_results() method.
$wpdb->flush();
// Check the connection:
if( ! $wpdb->check_connection( $allow_bail = false ) )
{
// Let\'s try to connect again, but there has already been
// reconnection retries within the check_connection() method above.
// Here we handle the bail manually:
// Check the connection:
if( ! $wpdb->check_connection( $allow_bail = false ) )
{
// Let\'s try to connect again, but there has already been
// reconnect retries within the check_connection() method above.
// Here we handle the bail manually:
if( ! $wpdb->db_connect( $allow_bail = false ) )
{
// Exit with a style:
if ( did_action( \'template_redirect\' ) )
{
die( __( \'No DB connection\' ) );
}
else
{
wp_load_translations_early();
$wpdb->bail( sprintf( \'<pre>%s<pre>\', __( \'No DB connection\' ) ) );
}
// Just in case:
dead_db();
}
}
}
// Fetch another set of data:
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->users}" );
请注意,在大多数情况下,我们不需要使用$wpdb
. 我们只需使用WP_Query
, WP_Comment_Query
或WP_User_Query
课程。它们可以帮助我们生成通过包含的$wpdb->get_results()
或$wpdb->get_col()
呼叫。
对于WP_Query
这种情况发生在humongousWP_Query::get_posts()
方法
所以我们已经有了$wpdb->flush()
呼叫。
结束