PHP bind_result仅返回null

时间:2019-07-16 08:24:54

标签: php mysql

我正在为http请求创建一个简单的API,但我不太了解在此特定查询语句的bind_result()中必须包含多少列,但是在phpMyAdmin中效果很好。

$stmt = $conn->prepare("SELECT 
            post.ID,
            post.post_title,
            post.post_content,
            post.post_date,
            post.category_name,
            post.category_slug,
            post.category_id,
            CONCAT( $domain,'/', thumb.meta_value) as thumbnail,
            post.post_type
        FROM (
            SELECT  p.ID,   
                  p.post_title, 
                  p.post_content, 
                  p.post_date,
                  p.post_type,
                  MAX(CASE WHEN pm.meta_key = '_thumbnail_id' then pm.meta_value ELSE NULL END) as thumbnail_id,
              term.name as category_name,
              term.slug as category_slug,
              term.term_id as category_id
            FROM wp_posts as p 
            LEFT JOIN wp_postmeta as pm ON ( pm.post_id = p.ID)
            LEFT JOIN wp_term_relationships as tr ON tr.object_id = p.ID
            LEFT JOIN wp_terms as term ON tr.term_taxonomy_id = term.term_id
               WHERE p.post_status = 'publish'
            AND p.post_type = 'post'
            AND term.name = 'news'
            GROUP BY p.ID ORDER BY p.post_date DESC
          ) as post
          LEFT JOIN wp_postmeta AS thumb 
            ON thumb.meta_key = '_wp_attached_file' 
            AND thumb.post_id = post.thumbnail_id
          LIMIT 20");

                $stmt->execute();
                $stmt->store_result();

                if($stmt->num_rows > 0){

                    $stmt->bind_result($ID, $post_title, $post_content, $post_date, $category_name, $category_slug, $category_id, $thumbnail, $post_type);

                    while (mysqli_stmt_fetch($stmt)){

                        $obj = array(
                            'post_title'=>$post_title, 
                            'post_content'=>$post_content,
                            'post_date'=>$post_date,
                            'category_name'=>$category_name,
                            'category_slug'=>$category_slug,
                            'category_id'=>$category_id,
                            'thumbnail'=>$thumbnail,
                            'post_type'=>$post_type
                        );

                        $list[] =  $obj;

                    }

                    $response['error'] = false;
                    $response['message'] = 'Success!';
                    $response['results'] = $list;

                    $stmt->close();

                }
                else...

这是列结果(请参见下图),所以我的绑定有什么问题? 顺便说一下,这是针对WordPress的。

enter image description here

0 个答案:

没有答案