$result = mysql_query("SELECT avg(r.rate) FROM rate r where ImgName='1'");
this php is not working.
Originally my code is
<?php
$con = mysql_connect("localhost","root","sql");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("photogallery", $con);
$result = mysql_query("SELECT avg(r.rate) FROM rate r ");
echo "<table border='0' cellspacing='5'>";
echo "<th> Average Rating </td>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> " . $row['rate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
the above is not showing any out put.
but modify code i.e. then its workin.
$result = mysql_query("SELECT r.rate FROM rate r ");
but i want to aggregate function
thanks in advance
you can use an alias:
SELECT avg(r.rate) AS rate_average
FROM rate r
WHERE ImgName='1'
and then output:
echo "<td> " . $row['rate_average'] . "</td>";
Your query is producing a scalar rather than a set of rows. If you want to get the average rate per item then you should do something like:
SELECT avg(r.rate) FROM rate r GROUP BY ItemIdColumn
And yes, if you want to fetch the value by column name, you should use an alias, like knittl mentioned.
Related
So what i want is the following. I have 3 tables: first i will explain in english what i want for final result:
let me explain all in english:
table 1 is REGISTERED_MEMBERS
table 2 is OPEN CLASSES
table 3 i want who appplied and to which class they applied and show that in a table when date is selected from a dropdown list!
This is how it should show at the end:
table = razpisani_tecaji with (ID_TECAJA, DATE, STATUS, ST_ODPRTIH_MEST)
table = registrirani_clani wtih (ID_TECAJNIKA,IME,PRIIMEK, EMAIL)
table = prijave_na_tecaj with (ID_TECAJNIKA, ID_TECAJA, PLACILO)
now in HTML i have a dropdown list populated with dates (this works ok) from table 1.
<form>
<select name="dates" onchange="showUser(this.value)">
<option value="" selected="selected">Izberi datum za pregled</option>;
<?php
$con = mysqli_connect('localhost','root','','viverius_education');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = mysqli_query($con, "SELECT ID_TECAJA, DATUM FROM razpisani_tecaji");
while ($row = $sql->fetch_assoc()){
echo "<option value='" . $row['ID_TECAJA'] . "'>" . $row['DATUM'] . "</option>";
}
?>
Here a ID is saved to a variable q which is then send to php via JS. This all works. Now what i would like is for user to select a date and get a results from 3. table only to show IME from linked to ID_TECAJNIKA and PLACILO linked to ID_TECAJNIKA.
THis is what i have in PHP so far:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','viverius_education');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql="SELECT razpisani_tecaji.ID_TECAJA, registrirani_clani._ID_TECAJNIKA FROM prijave_na_tecaj
LEFT JOIN razpisani_tecaji ON prijave_na_tecaj.ID_TECAJA = razpisani_tecaji.ID_TECAJA
LEFT JOIN registrirani_clani ON prijave_na_tecaj.ID_TECAJNIKA = registrirani_clani.ID_TECAJNIKA";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Ime</th>
<th>Placilo</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ID_TECAJNIKA'] . "</td>";
echo "<td>" . $row['PLACILO'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
$sql="select registrirani_clani., prijave_na_tecaj.,razpisani_tecaji.*
from registrirani_clani
left join prijave_na_tecaj on registrirani_clani.ID_TECAJNIKA = prijave_na_tecaj.ID_TECAJNIKA
left join razpisani_tecaji ON razpisani_tecaji.ID_TECAJA = prijave_na_tecaj.ID_TECAJA
where razpisani_tecaji.ID_TECAJA = '".$q."'";
I need my computer game to take all kills and deaths from a user's total and then make it so the kills are divided by the deaths, then that total is put at the end. This ratio is known as their kill-death ratio, or "KDR".
<?php
// Create connection
$con=mysqli_connect("ipaddress","user","password","minecraft");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT *, `kills`/`deaths` as `KDR` FROM war_kills ``ORDER BY kills DESC");
echo "<table border='1'>
<tr>
<th>Player</th>
<th>Kills</th>
<th>Deaths</th>
<th>KDR</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['KDR'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
We have this up so far: http://gexgaming.com/warstats/index.php
Change the query from
SELECT player, kills, deaths, KDR FROM war_kills ORDER BY kills DESC`
to
SELECT player, kills, deaths, kills / deaths as KDR FROM war_kills ORDER BY kills DESC`
Either modify the SQL query as suggested or do it within the PHP loop:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . ($row['kills'] / $row['deaths']) . "</td>"; // <--- this one
echo "</tr>";
}
It looks like you need to group your results based on the player name. Also, you need to account for when deaths = 0. Try changing your query to -
SELECT
player,
SUM(kills) as kills,
SUM(deaths) as deaths,
CASE WHEN SUM(deaths) = 0 THEN SUM(kills)
ELSE SUM(kills)/SUM(deaths)
END as `KDR`
FROM war_kills
GROUP BY player
ORDER BY kills DESC
The other answers I read don't account for the fact that someone might have zero deaths. If they do, you'll get a divide by zero error.
inside your while loop,
if ($row['deaths'] == 0)
$kdr = '-';
else
$kdr = $row['kills']/$row['deaths'];
then change $row['KDR'] to $kdr
<?php
$con = mysql_connect("localhost","root","root");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mydb",$con);
//Count all Total of Acc Class with same value Example Restaurant, Hotel
$query = "select acc_class,count(*) as total from mytable group by acc_class";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_total = $values['total'];
while($record = mysql_fetch_array($result)){
echo '<br>';
echo "<label>" . $record['acc_class'] . "</label>";
echo "<label>" . $num_total . "</label>";
}
mysql_close($con);
?>
Guys please help me. I want to produce something like this. But I don,t know how.
Account Class Total
------------- -----------
Hotel 5
Restaurant 2
Club 3
Church 1
I want to have a page in which it will show total numbers of each account class. Please help.
Thanks!
//you just need to put the name of the column in quert like COUNT(column_name)
$con = mysql_connect("localhost","root","root");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mydb",$con);
//data base connection ends here
//you write a query to fire if you want by order the put "ORDER BY HERE"
$query = "select acc_class,count(acc_class) as total from mytable group by acc_class";
//here you fire a query to mysql
$result = mysql_query($query);
//now put the selected roe in loop and again and again loop ask for more data to mysql
while($record = mysql_fetch_array($result)){
**//here you select all the group in the table**
echo '<br>';
echo "<label>" . $record['acc_class'] . "</label>";
echo "<label>" . $record['total'] . "</label>";
}
//close connection to database
mysql_close($con);
Please dump $values:
var_dump($values);
I guess, the problem is it's either the data itself or the data-fetch (array, assoc).
Please format your SQL for better readability:
SELECT acc_class, COUNT(*) AS total FROM mytable GROUP BY acc_class;
The Count statement is correct: https://dev.mysql.com/doc/refman/5.7/en/counting-rows.html
Solution:
$query = "SELECT acc_class, COUNT(*) AS total FROM mytable GROUP BY acc_class";
$result = mysql_query($query);
echo '<table border="1">';
while($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['acc_class'] . '</td>';
echo '<td>' . $row['total'] . '</td>';
echo '</tr>';
}
echo '</table>';
I want to display a table that looks like this from a Mysql query using PHP:
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
9 2 3 5
Vendor Name
Item Not to Drawing Item Defective Incorrect Item Received Other
2 4 5 7
etc..
Here is my code....
<?php
$con = mysql_connect("localhost","xxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("qa", $con);
$result = mysql_query("SELECT Vendor, Sum(draw), Sum(defective), Sun(received), Sum(other) FROM qa_reports Group by Vendor Order by Vendor ASC");
echo "<table>
<tr>
<th>Item not to Drawing</th>
<th>Item Defective</th>
<th>Incorrect Item Received</th>
<th>Other</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Sum(draw)'] . "</td>";
echo "<td>" . $row['Sum(defective)'] . "</td>";
echo "<td>" . $row['Sum(received'] . "</td>";
echo "<td'>" . $row['Sum(other)'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Not sure how to get the above results formatted as such.
I've done this with CFML but am new to PHP and can not grasp how to list results in a table grouped by a field.
You need to alias your columns in order to reference them in your $row array.
SELECT vendor,
Sum(draw) AS draw,
Sum(defective) AS defective,
Sun(received) AS received,
Sum(other) AS other
FROM qa_reports ...
Then you can reference them like so:
$row['draw'];
...
SELECT Vendor, Sum(draw) AS sumDraw, Sum(defective) AS sumDefective ...
$row['sumDraw'] etc.
i cannot get a row to delete as the id is not going through the url. its a simple error somewhere and i cannot find the solution after having a look around for an hour.
this page contains the information on a table:
<?php
$result = mysql_query("SELECT review, ratings, date, user FROM reviews")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Reviews Yet';
} else {
echo "<table border='0'><table width=100% border='6'><tr><th>Comments/Thoughts</th><th>Ratings</th><th>Date</th><th>User</th><th>Delete</th></tr>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['review']. "</td>";
echo "<td>" . $info['ratings']. " Stars</td>";
echo "<td>" . $info['date']. "</td>";
echo "<td>" . $info['user']. "</td>";
echo "<td>" . " <a href='deletereview.php?review_id=" . $info['review_id'] . "'>Delete</a> </td>";
echo "</tr>";
}
}
echo "</table>";
?>
it goes to deletereview.php which carries out the delete function:
<?php
session_start();
require_once '../includes/db.php';
$id = $_GET['review_id'];
$info = "DELETE FROM reviews WHERE review_id = '$id'";
mysql_query($info) or die ("Error: ".mysql_error());
echo "<h2>Review Deleted</h2>";
?>
any ideas guys?
You're not selecting the review_id in the query, so $info["review_id"] is always null.
Aside from the other answers, I'll say this:
Your database will get jacked if you do not sanitize your variables.
For instance, what happens if I pass review_id=' OR '1'='1?
DELETE FROM reviews WHERE review_id = '' OR '1'='1'
This query will delete everything in reviews.
mysql_real_escape_string() your $_GET and $_POST variables before using them in your MySQL.
You forgot to select the review_id.
$result = mysql_query("SELECT review_id, review, ratings, date, user FROM reviews")
You're not selecting review_id from the database but you use $info['review_id'] to set the ID on the URL. Just change your first line to:
$result = mysql_query("SELECT review_id, review, ratings, date, user FROM reviews")
Also you must escape the input with mysql_real_escape_string:
$id = mysql_real_escape_string($_GET['review_id']);
You have to select the review_id in the query. But also you have to check for some SQL injection, because with the GET request it's easy to delete all the table records.