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 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>
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
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
Related
I have code that takes values from a database that belong to a particular user.
<?php
$user = $_SESSION['username'];
$q = intval($_GET['q']);
$db = mysqli_connect('localhost', 'username', 'password', 'database_name');
$sql = "SELECT * FROM savedtimes WHERE username = '$user' AND session = '$q' ORDER BY timeid";
$result = mysqli_query($db, $sql);
//$SNo = I might need to put something over here which makes it show the order number.
echo "<table>
<tr>
<th>S.No.</th>
<th>time</th>
<th>Avg of 5</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $SNo . "</td>";//This is the S.no column.
echo "<td>" . $row['value2'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Now, while it is displaying these values in the order of a field called timeid, which is on auto increment, I want to have a column called S.No (only while displaying, not in the actual database), which orders them from 1 to the last number. Please note that I can't just use 'timeid', because I have multiple users, so the numbers won't be continuous. So basically, the order is that of 'timeid', but I want a column showing up with continuous numbers. How do I do this?
Declare a counter variable (with a value of 1) outside of the while() loop. Display it inside the loop and subsequently increment it at the end of the loop.
// your code
$SNo = 1;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $SNo . "</td>";
echo "<td>" . $row['value2'] . "</td>";
echo "</tr>";
++$SNo;
}
// your code
I am trying to get all values shown in a table from mysql but getting one .
I want to get the rows of the mysql table at in the last table mentioned in the code
////////Here is a desc of no use --- blah for just posting this question / as i am getting an error msg for giving more details information about this question /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the code:
$sql = 'SELECT
item_added
FROM
products_added
ORDER BY id';
$results = mysqli_query($conn, $sql);
if(mysqli_num_rows($results) < 1){
echo "No items";
}else{
$new_sql = 'SELECT
item_added,
quantity,
amount,
sum(amount) as items_total
FROM
products_added
where `username` = "'.mysqli_real_escape_string($conn, $_SERVER["REMOTE_ADDR"]).'"
ORDER BY id';
$resu = mysqli_query($conn, $new_sql);
}
?>
<table>
<thead>
<tr>
<td>Item</td>
<td>Qyt</td>
<td>Price</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($resu)){
echo "<tr>";
echo "<td>" . $row['item_added'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td><a class=\"remove-from-cart\" href=\"\"><i class=\"fa fa-times\"></i></a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
It looks like its because you're using an aggregate function SUM() without a GROUP BY. In the $new_sql query, try adding "GROUP BY item_added" right before "ORDER BY id".
I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.
$u=$_POST['userid'];
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");
echo "<table border='1'>
</tr>
<tr>
<th>User ID</th>
<th>count</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Try the following SQL Query:
SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;
Refer to the documentation of the GROUP BY clause.
Use below:
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name");
where clause use for filter the data.
refer http://www.w3schools.com/sql/sql_where.asp
I am still battelling to show data from two different tables on one screen. The tables both contain one common field name, all i want is to filter on that field name and be able to alter and display details from that line or lines based on that field name they share.
The two tables are:
members and recipients
In members it has a primary key member_id and it is unique. Then in recipients I created a member_id field that needs to link to the members table unique member_id as well.
The following code calls information from members table
<?php
$con = mysql_connect("localhost", "****", "****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stingin_epanic", $con);
$result = mysql_query("SELECT * FROM members WHERE member_msisdn='$slusername'");
echo "<table border='1'>
<tr>
<th>Membership</th>
<th>Number</th>
<th>Registration Date</th>
<th>End Date</th>
<th>Copy of ID</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<td><center>" . $row['member_id'] . "</td>";
echo "<td><font color=blue>" . $row['member_msisdn'] . "</td>";
echo "<td><center>" . $row['asdate'] . "</td>";
echo "<td><center>" . $row['aedate'] . "</td>";
echo "<td><center>" . $row['attid'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
This works great and gives me what i need. I am also calling member_id here to show the primary key.
On same page i am trying to call information related to the member from another table using the following code. Note that the information is supposed to be linked to the member_id of the first code
<?php
$con = mysql_connect("localhost", "****", "****");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stingin_epanic", $con);
$result = mysql_query("SELECT * FROM members a INNER JOIN recipients b ON member_id = member_id WHERE member_msisdn=$slusername");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Number</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<td><font color=blue>" . $row['recipient_name'] . "</td>";
echo "<td>" . $row['recipient_msisdn'] . "</td>";
echo "</tr>";
echo "<td><font color=blue>" . $row['recipient_name'] . "</td>";
echo "<td>" . $row['recipient_msisdn'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Using the second code i get no results as it is calling no information
Use mysqli instead of mysql functions
Use aliases in You SQL queries.
change this
"SELECT * FROM members a INNER JOIN recipients b ON member_id = member_id WHERE member_msisdn=$slusername"
on this
"SELECT * FROM members a INNER JOIN recipients b ON a.member_id = b.member_id WHERE member_msisdn=$slusername"
Maybe this will help.
here im getting problem with my join query..i dont know where is the problem whether my query is wrong or whatelse but the error its giving is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project\teacher\courses-list.php on line 46
heres my code please mend the code i cudnt find the problem.. :(
courses-list.php
<?php
if ($_SESSION["isteacher"])
{
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
echo "<table border='1'> <br />
<tr>
<th>ID:</th>
<th>Course Name</th>
<th>Description</th>
<th>Subject-ID</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>";
while($row = mysql_fetch_array($result)) // this is the error line
{
echo "<tr>";
echo "<td>" . $row['cid'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td><a href='courses-edit.php?id=" . $row['id']."'>EDIT</a></td>";
echo "<td><a href='courses-delete.php?id=" . $row['id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
here is the error
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
should be
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id=subjects.id)");
the error portion is
subjects WHERE (courses.subjects-id==subjects.id)");
here is the error ---------^^--------sould be =
also please avoid even dont use the mysql_* even the php manual show the message about that use the mysqli or PDO