<?php
$q2 = "SELECT SUM(crd)
FROM coe_courses
INNER JOIN student_record ON coe_courses.course_number =
student_record.course_number WHERE student_record.id =".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q2 ) ;
if($row = mysqli_fetch_array($result)){
echo $row["SUM(crd)"];
}
$q2a = "SELECT SUM(points) FROM student_record where student_record.id =".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q2a ) ;
if($row = mysqli_fetch_array($result)){
echo $row["SUM(points)"];
}
?>
I wrote this code but how I can make division for those statements at the end of this code
echo $row["SUM(crd)"] /$row["SUM(points)"];
First of all you should store those two values in different variables because as per your code you are overriding the $row variable so you will get only second result. After that you can simply do this.
if(!empty($b)) {
$ans = (int) ($a/$b);
}
Related
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
I'd like to echo all ID's from the user table but I can't get it to work. I tried this below and searched some stacks but I can't find the right answer.
$query = $db->query( "SELECT * FROM users" );
$num = $db->num( $query );
while( $array = $db->assoc( $query ) ) {
$active_ids = $array['id'];
}
$query = "SELECT u.username, u.habbo, count(*) AS n
FROM users u, timetable tt
WHERE u.id=tt.dj and u.id IN (".$active_ids.")
GROUP BY tt.dj";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
echo "<tr id='uren-table-row'>";
echo "<td width='60px'><div style='height:50px;padding-top:25px;overflow:hidden;float:left;margin-top:-25px;'><img style='margin-top:-28px;float:left;' src='http://habmoon.org/moonstream/dj?habbie={$row['habbo']}&head_direction=3&direction=2&size=b&hb=img&gesture=sml'></div></td>";
echo "<td width='200px' style='padding:0 15px;'>", $row['username'] ,"</td>";
echo "<td style='padding:0 15px;'>Heeft <b>", $row['n'] ,"</b> Uur gedraait deze week</td>";
echo "</tr>";
}
}
Remove count(*) AS n from your query. This will cause MySQL to return only 1 record.
You also have some errors in your first lines:
$active_ids = array();
while( $array = $db->assoc( $query ) ) {
$active_ids[] = $array['id'];
}
You forgot to define $active_ids and add [].
You need to make $active_ids a CSL. Currently you overwrite the id(s) on every iteration so you only getting the last one. You also need it separated by commas so use implode. Try:
while( $array = $db->assoc( $query ) ) {
$active_ids[] = (int)$array['id'];
}
$active_ids = implode(',', $active_ids);
i can't understand why echo don't displays value. Query return values in phpmyadmin sow query is fine. Please help
$query="SELECT `doctor_name` , `doctor_secondname`
FROM `doctors`
INNER JOIN `patients` ON `patients`.doctor_id = `doctors`.doctor_id
WHERE `patients`.patient_id = '{.$patient_id.}'" ;
$result = mysql_query($query) or die(mysql_error());
print_r($row);
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
echo "Your doctor is :";
echo $row['doctor_name'];
echo $row['doctor_secondname'];
}
Try This:
$query="SELECT doctor_name , doctor_secondname
FROM doctors
INNER JOIN patients ON patients.doctor_id = doctors.doctor_id
WHERE patients.patient_id = '".$patient_id."'" ;
I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
I'm trying to get the sum of a column.
My Schema is as below...
I'd like to get the SUM of 'bill'.
I have the following...
<?php
$uid = $_SESSION['oauth_id'];
$query = mysql_query("SELECT * FROM `users`, `income`, `outgoings` WHERE users.oauth_uid = '$uid' and income.user_id = '$uid' and outgoings.user_id = '$uid'") or die(mysql_error());
$result = mysql_fetch_array($query);
?>
Outgoings = <?php echo $result["SUM(total)"];
I'm not receiving any output, however. Can anybody see where I'm going wrong? There's definitely data in my table.
The SUM function must be used when deciding what to return from the select. Like so.
SELECT SUM(`bill`) FROM `users`, `income`, `outgoings` WHERE users.oauth_uid = '$uid' and income.user_id = '$uid' and outgoings.user_id = '$uid'
Try this if you don't want to do a SUM() query as already proposed by others:
<?php
$sum = 0;
while ($row = mysql_fetch_array($query)) {
$sum += $row['bill'];
}
?>
Outgoings = <?php echo $sum; ?>
But remember that you will need this if you want to reuse the same $query resultset:
<?php
mysql_data_seek($query , 0);
?>
Why not just query for the SUM directly? Like:
<?php
$uid = $_SESSION['oauth_id'];
$sum = mysql_query("SELECT SUM(`bill`) FROM `users` WHERE users.oauth_uid = '$uid'") or die(mysql_error());
?>
This query will get you the sum:
SELECT sum(bill) FROM `the_table`