简体   繁体   中英

Cannot retrieve value of extra query per record?

Im working on a table where data from users will be picked from database.

The problem is that im not being able to put two statements on my while condition because im getting data from different tables and rows.

@CODE

<?php
$con = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

$result = mysql_query("select * FROM users");

$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");

$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");

echo "<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
<tr>
    <th>Username</th>
    <th>Type</th>                                   
    <th>Email</th>
    <th>Last Login</th>
    <th>Last IP</th>
    <th>space_used</th>
    <th>total_files</th>
    <th>status</th>
</tr>";

while($row = mysql_fetch_array($result))
{
    echo "<tr>";
        echo "<td>" . $row['username'] . "</td>";
        echo "<td>" . $row['level'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['lastlogindate'] . "</td>";
        echo "<td>" . $row['lastloginip'] . "</td>";
        echo "<td>" . $space_used['space_used'] . "</td>";
        echo "<td>" . $total_files['total_files'] . "</td>";
        echo "<td>" . $row['status'] . "</td>";
    echo "</tr>";
}

echo "</table>";
mysql_close($con);
?>

What im getting is all the values of username, level, email, lastlogindate, lastloginip & status. The two that are not showing on my table are the ones of the second condition i want to implement: space_used and total_files.

What am i doing wrong? Im not an expert in php and the fact that i had a mix of tutorials and a script im chaning to get to this result had complicated a little bit.

Thanks

You want to aggregate per userId.

Pull these two query's into the while loop. Use the retrieved User's id to Make the aggregate query.

while($row = mysql_fetch_array($result))
{
    $space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId={$row['id']} AND file.statusId=1");
    $total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId={$row['id']} AND file.statusId=1");
    ...
 }

Combine them into one query.

$agragateDataResponce = mysql_query("SELECT SUM(fileSize) as size,
                                            COUNT(id) as count 
                                       FROM file 
                                      WHERE file.userId={$row['id']} AND 
                                            file.statusId=1");
$agragateData = mysql_fetch_array($agragateDataResponce);

Access by $agragateData['size'] and $agragateData['count'] ;

Then look up how to combine the remaining two SQL into one join :) . SO Question on topic

You need to rework your query, you can resume your 3 queries into a single query using JOIN :

  SELECT u.*, 
         SUM(f.fileSize) AS space_used, 
         COUNT(f.id) AS total_files
    FROM users u
    JOIN file f 
      ON f.userId = u.id AND f.statusId = 1
GROUP BY u.id

Then you can read it like this:

<?php
// your database info
$db_host = 'your MySQL server host';
$db_user = 'your username';
$db_pass = 'your password';
$db_name = 'your database name';
$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if($con->connect_error)
{
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

if (!$result = $con->query("SELECT u.*,
                                   SUM(f.fileSize) AS space_used, 
                                   COUNT(f.id) AS total_files
                              FROM users u
                              JOIN file f 
                                ON f.userId = u.id AND f.statusId = 1
                          GROUP BY u.id"))
{
    die('Select query error: ' . $con->error);
}
?>
<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
    <tr>
        <th>Username</th>
        <th>Type</th>                                   
        <th>Email</th>
        <th>Last Login</th>
        <th>Last IP</th>
        <th>space_used</th>
        <th>total_files</th>
        <th>status</th>
    </tr>
<?php
while ($row = $result->fetch_array())
{
?>
    <tr>
        <td><?php echo $row['username']; ?></td>
        <td><?php echo $row['level']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['lastlogindate']; ?></td>
        <td><?php echo $row['lastloginip']; ?></td>
        <td><?php echo $row['space_used']; ?></td>
        <td><?php echo $row['total_files']; ?></td>
        <td><?php echo $row['status']; ?></td>
    </tr>
<?php
}
$con->close();
?>
</table>

First you query the database and save the results into variables.

$result = mysql_query("select * FROM users");
$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");
$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");

Then you iterate through the first of your results with

while($row = mysql_fetch_array($result))

Notice your call to mysql_fetch_array . You never call mysql_fetch_array on the other two results.

See the PHP documentation on mysql_query for more details. Specifically, the return type is a resource .

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

However,

SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1

is not even what you want. You check for file.userId=users.id but only read from the file table. You are missing the users source table.

Try to use

$space_used = mysql_query("SELECT SUM(fileSize) 
                             FROM file 
                            WHERE file.userId=users.id AND 
                                  file.statusId=1");

$total_files = mysql_query("SELECT COUNT(id) 
                              FROM file 
                             WHERE file.userId=users.id AND 
                                   file.statusId=1");

the above lines inside the While and verify with the user.id

I am new to stackoverflow so don't mine. I don't know how to make a code look like every one do

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