WordPress函数中MYSQL_FREE_RESULT和MYSQL_PING的替代函数

时间:2015-05-14 作者:Thamaraiselvam

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_QueryWP_User_Query 课程。它们可以帮助我们生成通过包含的$wpdb->get_results()$wpdb->get_col() 呼叫。

    对于WP_Query 这种情况发生在humongousWP_Query::get_posts() 方法

    所以我们已经有了$wpdb->flush() 呼叫。

结束

相关推荐

Split posts between databases

我的客户要求为基于WP的wordpress(单站点,而不是多站点)提供一个非常具体的解决方案。基本上,他想要的是,假设在1000篇文章之后,应该创建新的数据库(只有5个表,posts,posmeta和3个与分类相关的表),新的文章应该和所有的post meta一起存储在这个数据库中,直到它达到1000篇文章,然后处理重复。具有挑战性的部分是,所有这些数据库必须同时处于活动状态,因此WP应该能够同时访问来自主数据库和所有这些新创建的数据库(有5个表)的帖子。这可能需要hyperdb. 问题包括:如何告诉wp