Home » Php » php – Can't combine two queries into one json object

php – Can't combine two queries into one json object

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to combine multiple queries into one result set that I can json_encode. I’m trying to get the user’s article and article topic in the first query and the amount of likes in the second query. I’m using AJAX to retrieve the data from the database. My AJAX code looks like this, which doesn’t seem to be having any problems:

$.ajax({
    url: 'includes/articles/getArticlePreview.php',
    type: 'POST',
    data: {articleId: articleId, idUsers: idUsers},
    dataType: 'json',
    success: function(data) {
        data.forEach(function(item) {

            console.log(item.voteCount);
            console.log(item.article);
            console.log(item.topic);

        });
    }
});

and on the back end, I did two separate queries and tried to combine them into one JSON object. My first query is like this:

include '../dbh.inc.php';

$idUsers = mysqli_real_escape_string($conn, $_POST['idUsers']);
$articleId = mysqli_real_escape_string($conn, $_POST['articleId']);

$q = "SELECT COUNT(*) FROM articlevotes WHERE articlevotes.article_id = '$articleId'";
$r = mysqli_query($conn, $q);
while ($row = mysqli_fetch_array($r)) {

    $voteCount = $row['COUNT(*)'];
    $data['voteCount'] = $voteCount;

}

This returns the vote count just fine. Then, I add my second query:

$query = "SELECT articles.article, articles.topic FROM articles WHERE articles.article_id = '$articleId' AND articles.idUsers = '$idUsers'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {

    $article = $row['article'];
    $topic = $row['topic'];
    $data['article'] = $article;
    $data['topic'] = $topic;

}

This works fine on its own, as well (when I delete the first query and only encode this one). But when I try to put them all in a $results[] = $data; array and echo json_encode($results);, I’m only getting the “voteCount” in the JSON object and when I go to the network and click on the file that contains the queries, I get the error messages:

Notice: Undefined index: idUsers in C:\xamp\htdocs\Real Website\includes\articles\getArticlePreview.php on line 5

Notice: Undefined index: articleId in C:\xamp\htdocs\Real Website\includes\articles\getArticlePreview.php on line 6
[{"voteCount":"0"}]

Here’s the whole query put together:

<?php

include '../dbh.inc.php';

$idUsers = mysqli_real_escape_string($conn, $_POST['idUsers']);
$articleId = mysqli_real_escape_string($conn, $_POST['articleId']);

$q = "SELECT COUNT(*) FROM articlevotes WHERE articlevotes.article_id = '$articleId'";
$r = mysqli_query($conn, $q);
while ($row = mysqli_fetch_array($r)) {

    $voteCount = $row['COUNT(*)'];
    $data['voteCount'] = $voteCount;

}

$query = "SELECT articles.article, articles.topic FROM articles WHERE articles.article_id = '$articleId' AND articles.idUsers = '$idUsers'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {

    $article = $row['article'];
    $topic = $row['topic'];
    $data['article'] = $article;
    $data['topic'] = $topic;

}

$results[] = $data;
echo json_encode($results);

I believe I’ve combined queries like this before, so I’m not sure what I’m doing wrong here. Additionally, is there a better alternative to this than doing two separate queries? Something to combine them in one query? Thanks.

How to&Answers: