douye1940 2019-07-08 17:17
浏览 156
已采纳

Wordpress数据库连接错误'该网站遇到技术问题。'

DB newbie hack here. I slogged my way through coding up a WP template file that retrieves the post data from another site's WP DB. It worked and was functioning fine for about 2 years and suddenly stopped working - crashing and not allowing the page to finish loading. Getting an 'The site is experiencing technical difficulties.' error.

I commented out the ::prepare method to try and isolate the issue and the page finished loading but still no data from the posts... again full novice here so I'm sure a seasoned vet could probably see the issue. Thanks for any insight!

<?php
                $originaldb = new wpdb('XXXXXX','XXXXXX','XXXXXX','XXXXXXXX');
                //Home Recent POST
                $post_id_recent = get_field('recent_post_id'); 
                $prep_recent = $originaldb->prepare("SELECT * FROM wp_posts WHERE $wpdb->posts.ID = $post_id_recent");
                $recent = $originaldb->get_results($prep_recent);

                $prep_recent_thumb = $originaldb->prepare("
                SELECT p1.*, wm2.meta_value FROM wp_posts p1 LEFT JOIN
                wp_postmeta wm1 ON (
                wm1.post_id = $post_id_recent
                AND wm1.meta_value IS NOT NULL
                AND wm1.meta_key = '_thumbnail_id'
                )
                LEFT JOIN wp_postmeta wm2 ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file'
                AND wm2.meta_value IS NOT NULL) WHERE p1.post_status='publish' AND p1.post_type='post'
                "); 
                $recent_thumb = $originaldb->get_results($prep_recent_thumb);
                foreach ($recent_thumb as $obj) :
                    $url = $obj->meta_value;
                endforeach; ?>

                <?php foreach ($recent as $obj) : ?>
                <div class="home_recent_thumb" style="background-image:url(<?php echo 'http://kuteblackson.com/blog/wp-content/uploads/'.$url; ?>)" onclick="location.href='<?php echo $obj->guid; ?>';"></div>
                <div class="home_recent_content">
                    <div class="home_recent_title"><a href="<?php echo $obj->guid; ?>"><?php echo $obj->post_title; ?></a></div>
                    <div class="home_recent_excerpt"><?php echo $obj->post_excerpt; ?></div>
                    <a href="<?php echo $obj->guid; ?>" class="button medium radius home_blog_button">READ BLOG</a><br>
                    <a href="blog" style="font-size: 16px; color: #9e9898; text-decoration:underline;">See all blog posts</a>
                </div><!-- end .home_recent_content -->
                <div style="clear:both;"></div>
                <?php endforeach; wp_reset_query(); ?>
  • 写回答

1条回答 默认 最新

  • dtjzpg5313 2019-07-08 17:42
    关注

    You're not using the prepare() method correctly.

    From the documentation:

    Parameters

    $query (string) (Required) Query statement with sprintf()-like placeholders

    $args (array|mixed) (Required) The array of variables to substitute into the query's placeholders if being called with an array of arguments, or the first variable to substitute into the query's placeholders if being called with individual arguments.

    $args,... (mixed) (Required) further variables to substitute into the query's placeholders if being called wih individual arguments.

    In your code, both calls to the prepare() method are passing only the first parameter (the query) and the method expects at least two of them which is why the code is triggering an error and so you get that "The site is experiencing technical difficulties" message.

    This is how your prepare() calls should look like:

    $prep_recent = $originaldb->prepare(
        "SELECT * FROM wp_posts WHERE $wpdb->posts.ID = %d",
        array($post_id_recent)
    );
    
    $prep_recent_thumb = $originaldb->prepare("
        SELECT p1.*, wm2.meta_value FROM wp_posts p1 LEFT JOIN
        wp_postmeta wm1 ON (
            wm1.post_id = %d
            AND wm1.meta_value IS NOT NULL
            AND wm1.meta_key = '_thumbnail_id'
        )
        LEFT JOIN wp_postmeta wm2 ON (
            wm1.meta_value = wm2.post_id 
            AND wm2.meta_key = '_wp_attached_file'
            AND wm2.meta_value IS NOT NULL
        ) 
        WHERE p1.post_status='publish' AND p1.post_type='post'",
        array($post_id_recent)
    );
    

    Notice that the $post_id_recent variable is being passed to the prepare() method as the second parameter in both cases. This should fix the issue (assuming that's the only problem.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗