简体   繁体   中英

How to retrieve two results from SQL Database

I am using PHP server with SQL Database using CPanel.

I am retrieving values (numbers) from the database using SQL queries and pass them to a separate javascript file. In order to achieve that, I first created a query on a separate PHP file which gets the information:

File Name: SQLamountofusers.php

<?php
$query = sprintf("SELECT COUNT( * ) as c
FROM  `applications` 
JOIN  `users` ON ( users.id = applications.id_m ) 
WHERE  `cdate` > CURRENT_DATE -30"); 

$result = mysql_query($query);

$res = mysql_fetch_assoc($result);
echo json_encode($res[c]);

mysql_free_result($result);
?>

The echoed result is then passed to a JS file using AJAX:

File Name: canvas.js

$(document).ready( function() {
    $(usersreport).click(function() {
        $.ajax({
            type: 'POST',
            url: 'js/SQLamountofusers.php',
            data: 'id=usersreport',
            dataType: 'json',
            cache: false,
            success: function(result) {

            var num = Number(result);

            { label: "Active",  y: num},
            { label: "Inactive",  y: 0  },

    });
});

Now this is what I want to achieve but don't know how:

I want to pass to the canvas.js file two values, one is the active users (which requires one query from the DB, which is already written above) and the other is the inactive users (which requires another query) - So how can I get two results into the JS file?

Should I create another separate PHP file with the other query or is there another way to achieve it?

Try adjusting your query to something like:

<?php
$query = sprintf("SELECT
    COUNT(DISTINCT IF(cdate > CURRENT_DATE - 30, users.id, NULL)) AS c,
    COUNT(DISTINCT IF(cdate <= CURRENT_DATE - 30, users.id, NULL)) AS d
FROM applications
    JOIN users ON users.id = applications.id_m");

$result = mysql_query($query);

$res = mysql_fetch_assoc($query);
echo json_encode($res);

mysql_free_result($result);
?>

Then you can adjust your canvas.js to pull the result as

$(document).ready( function() {
    $(usersreport).click(function() {
        $.ajax({
            type: 'POST',
            url: 'js/SQLamountofusers.php',
            data: 'id=usersreport',
            dataType: 'json',
            cache: false,
            success: function(result) {

            var num = Number(result.c);
            var num2 = Number(result.d);

           { label: "Active",  y: num},
           { label: "Inactive",  y: num2 },

     });
});

Disclaimer: I have not tested this code, just offering this as a potential solution you may try.

File Name: SQLamountofusers.php

<?php
$query = "SELECT COUNT( * ) as c
FROM  `applications` 
JOIN  `users` ON ( users.id = applications.id_m ) 
WHERE  `cdate` > CURRENT_DATE -30"; 

$result = mysql_query($query);

$res = mysql_fetch_assoc($result);
$data = array('active' => $res['c']);

$query2 = "SELECT ...";
$result2 = mysql_query($query2);
$res2 = mysql_fetch_assoc($result2);
$data['inactive'] = $res2['c']);
echo json_encode($data);
?>

File Name: canvas.js

$(document).ready( function() {
    $(usersreport).click(function() {
        $.ajax({
            type: 'POST',
            url: 'js/SQLamountofusers.php',
            data: 'id=usersreport',
            dataType: 'json',
            cache: false,
            success: function(result) {

            { label: "Active",  y: result.active},
            { label: "Inactive",  y: result.inactive},

    });
});

Just replace second sql query with the one you need.

EDIT: Basically you can pass through json arrays and objects. So you can easily pass as many numbers as you want. Prepare php array with values you need, then json_encode() it and you will get an object or array with values on js side. If you encode php array without keys like $arr = array(1,2,3); you will get array on js side. You can reference it as a[0], a[1] etc. If you encode associative php array, ie with keys like $arr = array('k1'=>1, 'k2'=>2); you will get an object on js side. And reference it as o.k1, o.k2 etc.

Try: $query = "SELECT COUNT( * ) as c FROM applications JOIN users ON ( users.id = applications.id_m ) WHERE cdate > CURRENT_DATE -30";

Without sprintf();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM