php - mysql PHP PDO在单个POST请求上执行2个查询

php - mysql PHP PDO在单个POST请求上执行2个查询,第1张

我正在使用标准apache2 LAMP配置mysqlphp5,我改变的只是我的字符编码到UTF8

点击一个主播(文章标题)后,我正在进行AJAX调用,并希望运行2个查询。一个获得文章,另一个获得与文章相关的评论。

我在浏览时发现this post,但到目前为止还没能实现他所说的。我收到此消息:Fatal error: Call to a member function nextRowset() on a non-object in /home/shooshte/Dropbox/PTC_php/db_queries/articles.php on line 45。我实际上不知道这是否是正确的方法。

问题是第一个SELECT语句返回一行(一篇文章),而第二个SELECT语句返回多行(该文章的所有注释),所以我无法将它们真正地连接成一个语句。 / p>

无论如何,这是我的代码(我注释掉了不相关的部分):

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

try {
    $hostname = "localhost";
    $username = "topdecka_admin";
    $password = "";

    $db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC;charset=utf8",$username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    /*if (!empty($_POST["searchword"])) {
        $searchword = $_POST["searchword"];
        $query = $db->prepare(
            'SELECT articles.title, articles.posted, articles.extract, authors.name, GROUP_CONCAT(categories.cat_name) AS cat_name
            FROM articles, authors, categories, article_categories 
            WHERE articles.author_id = authors.id
            AND articles.id = article_categories.article_id
            AND article_categories.category_id = categories.id
            AND ((title LIKE :searchword) OR (extract LIKE :searchword) OR (body LIKE :searchword) OR (name LIKE :searchword) OR (cat_name LIKE :searchword))'
            ); //end DB QUERY
        $query->execute(array(":searchword" => "%" . $searchword . "%"));
        $result = $query->fetchAll();
        //turns timestamp into integer
        for($i = 0; $i < count($result);   $i) {
          $result[$i]['posted'] = strtotime($result[$i]['posted']);
        }
        echo json_encode($result);
        die(); */
    } 
    else if (!empty($_POST["title"])) {
        $title = $_POST["title"];
        $query = $db->prepare(
            "SELECT articles.title, articles.posted, articles.body, authors.name, authors.img, authors.bio, GROUP_CONCAT(categories.cat_name) AS cat_name
            FROM articles INNER JOIN authors ON articles.author_id = authors.id
            INNER JOIN article_categories ON articles.id = article_categories.article_id
            INNER JOIN categories ON article_categories.category_id = categories.id
            WHERE title LIKE :title; SELECT comment.user_id, comment.text, comment.article_id 
            FROM articles RIGHT JOIN comment ON articles.id = comment.article_id
            WHERE title LIKE :title; SELECT comment.user_id, comment.text, comment.article_id FROM articles RIGHT JOIN comment ON articles.id = comment.article_id;"
            ); //end DB QUERY
        $query->execute(array(":title" => $title));
        $result = $query->fetchAll();
        $result->nextRowset();
        $result[0]['posted'] = strtotime($result[0]['posted']);

        echo json_encode($result);
        die();
    }
    /*else {
        $query = $db->prepare(
            'SELECT articles.title, articles.posted, articles.extract, authors.name, GROUP_CONCAT(categories.cat_name) AS cat_name
            FROM articles, authors, categories, article_categories 
            WHERE articles.author_id = authors.id
            AND articles.id = article_categories.article_id
            AND article_categories.category_id = categories.id'
            ); //end DB QUERY
        $query->execute();
        $result = $query->fetchAll();
        //turns timestamp into integer
        for($i = 0; $i < count($result);   $i) {
          $result[$i]['posted'] = strtotime($result[$i]['posted']);
        }
        echo json_encode($result);
        die();
    } */
} 
catch (PDOException $e) {
    echo "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

这是发出POST请求的jQuery:

    $(".article-box h1 a").click(function(e) {
        e.preventDefault();
        var title = "title="  $(this).text();
        $.post(
            "db_queries/articles.php",
            title,
            function(data) {
                console.log(data);
                var response = JSON.parse(data);
                // formats article body
                var articleBody = response[0].body.trim();
                articleBody = articleBody.replace(/(\r\n|\n|\r)/gm,"<br>");
                // formats date
                var posted = new Date((response[0].posted * 1000));
                $("#articles-feed").empty();
                $("#articles-feed").append(
                    '<div class="article-box"><h1>'  response[0].title 
                    '</h1><h3>'   posted.toLocaleDateString() 
                    '</h3><p>'   articleBody  
                    '</p><button>Back to articles</button></div>'
                ); //end article append
                var authorHTML = (
                    '<img id="author-img" src="'   response[0].img  
                    '"><h2 id="author-name">'   response[0].name  
                    '</h2><p>'   response[0].bio  
                    '</p>');
                $("#author").append(authorHTML);
                //back to articles button
                $("#articles-feed button").click(function(e) {
                    e.preventDefault();
                    window.location.assign('articles');
                })
            } //end response function
        ); //end POST request
    }) //end article title click function

最佳答案:

1 个答案:

答案 0 :(得分:0)

现在有两个问题:您在包含数据的数组上调用nextRowset(),而您没有从第二行设置中获取数据。

您需要以下内容:

$result = $query->fetchAll();
// now $result contains your row with data

// get the next row set from the query
$query->nextRowset();
^^^^^^

// get the results of the second row set
$result2 = $query->fetchAll();

// You would have to see how you want to send both result sets...
echo json_encode( array('article' => $result, 'comments' => $result2) );
die();

请注意,您必须调整javascript以适应数据结构。

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复