I have a MySQL database that holds details on various video games and user accounts. A user is able to review a game and the score is put into a review table.
I am trying to retrieve the average score for each game and have it displayed on a webpage. My SQL statement within PHP looks like this
$sqlOverall = "SELECT CAST(AVG('scoreOverall') AS DECIMAL('10,1'))
FROM 'ratings'
WHERE gameID = '$id'";
$scoreOverall = mysqli_query($conn, $sqlOverall);
But when I try to echo $scoreOverall it returns blank.
SELECT CAST(AVG(scoreOverall) AS DECIMAL(10,1))
FROM ratings
WHERE gameID = '$id';
Try above code.
Hope this will helps.
try this below query
$selquery="SELECT CAST(AVG(scoreOverall) AS DECIMAL(10,1)) FROM ratings WHERE gameID='$id'";
$result= mysqli_query($conn, $selquery);
It works fine. Hope it is your helpful
$sqlOverall="SELECT CAST(AVG(scoreOverall) AS DECIMAL(10,1)) FROM ratings WHERE gameID=$id";
$scoreOverall = mysqli_query($conn, $sqlOverall);
foreach ($scoreOverall as $value) {
$scoreOverall = $value['CAST(AVG(queue_status) AS DECIMAL(10,1))'];
echo $scoreOverall;
}
Related
I am building a friends list of logged in user verified by session id. I have code that query's, results, and loops perfectly. BUT, it is posting an integer (maybe the id#?), (as this is my unique identifying condition of the select query) AND what I want is to output the (users) table rows of "firstname" and "lastname" instead of the user-id!
Sounds simple enough, yet, I lack the knowledge to make it work! So, here I am inquiry from the world of coders for help! Any assistance would be greatly appreciated!
Here is my code:
// Get Friend Array
$sql = "SELECT * FROM users
INNER JOIN friends
ON users.id = friends.id
WHERE (friends.user1='$u' OR friends.user2='$u' AND friends.accepted='1')";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
array_push($my_friends, $row["user2"]);
array_push($my_friends, $row["user1"]);
}
//remove your id from array
$my_friends = array_diff($my_friends, array($u));
//reset the key values
$my_friends = array_values($my_friends);
mysqli_free_result($query);
// Loop through $my_friends array and build results
foreach($my_friends as $friends => $v2);
$friends .= ' ';
}
Here is my html code:
<ul data-role="listview" data-autodividers="true">
<?php
echo "<li>$friends</li>";
?>
</ul>
It is the last line of code
$friends .= ' ';that I'm trying to workout. How the syntax of that line should be to add ($row["users.firstname"] and $row["users.lastname"]) and somehow not list the user "$id #" that is joining the two tables of (users) and (friends) in the select query!
It may be that I need a different array altogether for what I want, and If you know of the right way to do this, please inform me of how to do it...Thank you all!
What I understood that you are missing User's id in row. This issue is because php array can not have two keys with same name.
Change you query little bit like below
$sql = "SELECT *, friends.id as friendsID FROM users
INNER JOIN friends
ON users.id = friends.id
WHERE (friends.user1='$u' OR friends.user2='$u' AND friends.accepted='1')";
Now you will have id' as User Id andfriendsID` as Friend's ID
It's been long that I've been encountering this problem and I'll be glad if you guys can help me out on what to do.
This is the illustration.
Take for instance, you have a table called matrics and the table has a column called 'matric_num'.
Now, you create a registration page for users where one of the field is to select any matric num and after the user might have selected a matric number, filled the rest of the form and click on the submit button, it should insert the details into database.
If another user want to register, the matric number that the previous user selected should not be listed in the option menu.
HERE is what I did.
$query = mysqli_query($con, "SELECT *FROM `users`");
$rows = mysqli_num_rows($query);
echo "<select>";
for($i=1;$i<=$rows;$i++)
{
$fetch_user_matric = mysqli_fetch_array($query);
$matric_num = $fetch_user_matric['matric_num'];
$matric_query = mysqli_query($con, "SELECT *FROM `matrics` WHERE `matric_no` <> '$matric_num'");
$matric_rows = mysqli_num_rows($matric_query);
for($j=1;$j<=$matric_rows;$j++)
{
$fetch_matric_num = mysqli_fetch_array($matric_query);
$matric_no = $fetch_matric_num['matric_no'];
echo "<option value='$matric_no'>$matric_no</option>";
}
}
echo "</select>";
It displayed only the first matric number from the database whereas all matric numbers that hasn't been used by any user should be displayed.
Please what do you think is wrong with the sql query.
THANKS.
Just try this query:
SELECT * FROM `matrics` WHERE `matric_no` not in (select `matric_num` from `users`)
I'm trying to get my head around how I would do a particular MYSQL query but can't figure it out.
I have a table called developers and a table called plots. All of the plots have a developer id which links back to a developer name in the developers table.
I'm trying to output the developer name and then all the plots numbers under that developer. Once it's done that, I want it to do the same again with any more developers that may exist.
I've tried using joins however it will simply print:
developer name, plot number,
developer name, plot number,
developer name, plot number,
I only need the developer name to display once. However I need to print all the plot numbers.
I thought about having some kind of IF statement in the while loop where if the developer name is the same as it was previously in the last loop then it wont print. However I can't seem to figure out how that would work.
Thank you all for your help.
you can do in two steps
1. to get developer name and id.
2. get all plots of that id
$sql1 = "SELECT developer_id, developer_name FROM developers ";
$res = mysqli_query($con, $sql1);
while($row = mysqli_fetch_array($res))
{
$developer_id = $row['developer_id'];
$developer_name = $row['developer_name'];
echo $developer_name;
$sql2 = "SELECT * FROM plots WHERE developer_id='$developer_id' ";
$res2 = mysqli_query($con, $sql2);
while($row2 = mysqli_fetch_array($res2))
{
echo $row2['plot_id'];
}
}
This won't give you an output as exactly u need. But maybe this would do the job.
select developer_name, group_concat(plot_number SEPARATOR ',') from developer inner join plot on plot.developer_id = developer.developer_id group by developer_name
You can then explode out(PHP Explode with comma as the delimiter) the plot_number column from the result set to show all the plot numbers corresponding to a developer.
Use a Left Join..
Select * From developers Left Join plots on developers.developerid=plots.developerid
So it'll select everything from table developers and for each developer, it should select the plots.
Use an if condition to check if the last result of the developer id is equal to the current. If it is no, print the id.
Edit:
Here's how the PHP program should run..
$strSQL = "Select * From developers Left Join plots on developers.developerid=plots.developerid";
$Result = mysql_query($strSQL);
$DevID = "";
while ($Fields = mysql_fetch_array($Result))
{
if ($Fields["developerid"] != $DevID)
{
echo $Fields["developerid"];
$DevID = $Fields["developerid"];
}
echo $Fields["plot_column"];
}
How do I echo the latest values in column1? The below code echos the values before the update.
while($line = mysql_fetch_array($result)) {
$Student = $line["calss8"];
$querySf = "SELECT SUM(ABC) AS val1 FROM tbl1 WHERE student = '$Student'";
$resultSf = mysql_query($querySf);
$rSf = mysql_fetch_array($resultSf);
$totalSf = $rSf['val1'];
$totTMonth = $totalSf;
mysql_query("UPDATE tbl4 SET column1 = $totTMonth WHERE student = '$Student' LIMIT 1");
}
echo $line["column1"].",,";
As far as I know, you'll have to make a separate query to see what was just updated. I mean, run your select, perform your update, then do another select. You can get general information like how many rows were updated, but I don't think you can get specific information like the changed values in a column. Phil was right in suggesting that you should just print out the '$totTMonth' value since that is what you are updating your column with. That would be less overhead than doing another query to the database.
I think that problem starts before the code above. This code line will display the select results :echo $line["column1"].",,";. The variable $line is set before the code above. My solution is to do the following:
$result1 = mysql_query("SELECT column1 FROM student ..."); /* I insert the select query here */
While($row= mysql_fetch_array($result)) {
echo $row['column1'].",,";
}
am new to PHP. my question is i have a table with values and i want to sum the values of one of the column but its not working pls i would appreciate if you can help me with this
mysql_select_db($database_ebsConn, $ebsConn);
$transact4 = "SELECT sum(credit) FROM account WHERE integral_no ='$intergra'";
$transact5 = mysql_query($transact4, $ebsConn) or die(mysql_error());
$transact6 = mysql_fetch_assoc($transact5);
pls i am waiting for your reply
try to go with prepared Statements.... at least use separate database file
$sqlSEND=mysql_query("SELECT sum(credit) FROM account WHERE integral_no ='$intergra'");
while($result = mysql_fetch_assoc($sqlSEND)){
echo $result['sum(credit)'];
}