Print two columns array into HTML table - php

I have the following Query that I have to display in a HTML table:
$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);
When running the query in PHPMYADMIN, it works perfectly as I need but I can't figure out how to get the same result on a html table.
That's how is displayed in phpmyadmin:
Any help will be much appreciated.

$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);
echo "<table><tr><td>SUMA</td><td>country</td></tr>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr><td>".$row['suma']."</td><td>".$row['country']."</td></tr>";
}
echo "</table>";
mysqli_free_result($result);

$query= "SELECT SUM(amount_pln) AS suma,country FROM fin_revenues GROUP BY country ORDER BY suma DESC";
$result = mysqli_query($mysqli,$query);
echo '<table>
<tr>
<td>Suma</td>
<td>Country></td>
</tr>';
while($row=mysqli_fetch_array($mysqli,$result)) // while there are records
{
echo "<tr><td>".$row['suma']."</td><td>".$row['country']."</td></tr>"; // echo the table with query results
}
echo '</table';

Related

How to mysqli_fetch_row while using mysqli_multi_query

I'm trying to get the latest info about some specific person, and I'm using a query like
SELECT * FROM Table WHERE Name LIKE 'Peter' ORDER BY ID DESC LIMIT 1
and
SELECT * FROM Table WHERE Name LIKE 'Mary' ORDER BY ID DESC LIMIT 1
because in the Table each day will insert new data for different person at the instant of updating it (for record reference), so I would like to print out a few persons latest info by "ORDER BY ID DESC LIMIT 1"
I have tried to print it out with "mysqli_multi_query" and "mysqli_fetch_row"
like
$con=mysqli_connect($localhost,$username,$password,'Table');
$sql = "SELECT * FROM Table WHERE Name LIKE 'Peter' ORDER BY ID
DESC LIMIT 1 ";
$sql .= "SELECT * FROM Table WHERE Name LIKE 'Mary' ORDER BY ID
DESC LIMIT 1";
// Execute multi query
if (mysqli_multi_query($con,$sql))
{
do
{
// Store first result set
if ($result=mysqli_store_result($con)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
echo '<tr>'; // printing table row
echo '<td>'.$row[0].'</td>';
echo '<td>'.$row[1].'</td>';
echo '<td>'.$row[2].'</td>';
echo '<td>'.$row[3].'</td>';
echo '<td>'.$row[4].'</td>';
echo '<td>'.$row[5].'</td>';
echo '<td>'.$row[6].'</td>';
echo '<td>'.$row[7].'</td>';
echo '<td>'.$row[8].'</td>';
echo '<td>'.$row[9].'</td>';
echo '<td>'.$row[10].'</td>';
echo '<td>'.$row[11].'</td>';
echo '<td>'.$row[12].'</td>';
echo '<td>'.$row[13].'</td>';
echo '<td>'.$row[14].'</td>';
echo'</tr>'; // closing table row
}
// Free result set
mysqli_free_result($result);
}
}
while (mysqli_next_result($con));
}
mysqli_close($con);
?>
In the result page , it doesn't show any error message , but no results are printed.
The individual queries were tested.
Please advise, much thanks
Is there another way to keep the query simple, so there is no need to use mysqli_multi_query?
Best practice indicates that you should always endeavor to make the fewest number of calls to the database for any task.
For this reason, a JOIN query is appropriate.
SELECT A.* FROM test A INNER JOIN (SELECT name, MAX(id) AS id FROM test GROUP BY name) B ON A.name=B.name AND A.id=B.id WHERE A.name IN ('Peter','Mary')
This will return the desired rows in one query in a single resultset which can then be iterated and displayed.
Here is an sqlfiddle demo: http://sqlfiddle.com/#!9/2ff063/3
P.s. Don't use LIKE when you are searching for non-variable values. I mean, only use it when _ or % are logically required.
This should work for you:
$con = mysqli_connect($localhost,$username,$password,'Table');
// Make a simple function
function personInfo($con, $sql)
{
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<table>
<tr>';
for($i=0; $i < count($row); $i++) echo '<td>'.$row[$i].'</td>';
echo '</tr>
</table>';
}
}
}
$sql1 = "SELECT * FROM Table WHERE Name='Peter' ORDER BY ID DESC LIMIT 1 ";
$sql2 = "SELECT * FROM Table WHERE Name='Mary' ORDER BY ID DESC LIMIT 1";
// Simply call the function
personInfo($con, $sql1);
personInfo($con, $sql2);

How to use a $result variable with table object in SQL query using mySQLi

I am trying to make this code work, but it only works until the second echo statement echo "Finished 2";.
<?php
if (count($_GET) > 0){
$sql = "SELECT * FROM winery WHERE winery_name='".$_GET['winery_name']."'";
echo "Finished 1";
$result = $db->query($sql);
echo "Finished 2";
$sql = "SELECT * FROM".$result."WHERE wine_type='".$_GET['wine_type']."'";
echo "Finished 3";
$result = $db->query($sql);
echo "Finished 4";
$sql = "SELECT * FROM".$result.", wine_variety WHERE wine_id=wine_variety.wine_id";
echo "Finished 5";
$result = $db->query($sql);
echo "Finished 6";
$sql = "SELECT * FROM".$result."WHERE variety_id='".$_GET['grape_variety']."'";
echo "Finished 7";
$result = $db->query($sql);
echo "Finished all queries";
}
?>
The problem from my understanding is that sql doesn't recognize $result as a table, but $result stores the return table from my query. How can I make SQL use the return table from $result in a new query?
I think from your winery table you are fetching other table name???
If so you need to fetch row from the $result and then get appropriate column from winery table (i.e. column with other table name).
BTW best option would be joining two tables.
One more point where I think you are making mistake is
$sql = "SELECT * FROM".$result."WHERE wine_type='".$_GET['wine_type']."'";
should be
$sql = "SELECT * FROM ".$result." WHERE wine_type='".$_GET['wine_type']."'";
space between FROM & double quote and between double quote and WHERE
To get winery_id from winary_name you can write your HTML form like
<select name="winary_id">
<option value="Winary ID HERE">Winary Name Here</option> // you can generate your dynamic options like this which will return id instead of name
</select>

MySQL Selecting A Key From A Seperate Table

I am currently fetching data from a group of location MySQL tables here is what I currently have
$query = "SELECT Name, CountryCode, Population FROM City WHERE (Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "</tr>\n";
}
mysql_close($link);
The thing I am trying to figure out is how to convert the CountryCode to the actual country. Here is how I would call the country table to get the name
SELECT Name FROM Country WHERE Code = 'CountryCode';
How would I combine these two statements into one?
EDIT: Here is what I got to work. Is this considered a JOIN?
$query = "SELECT a.Name, a.CountryCode, a.Population, b.Code, b.Name FROM City a, Country b WHERE (a.CountryCode = b.Code) AND (a.Population > '6000000') ORDER BY Population DESC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[4]</td>";
echo "</tr>\n";
}
mysql_close($link);
U need to join City table and Country table having one primary key and one foreign key.
Read about Natural Join. It's easy.
You can use this query:
$query = "SELECT city.Name,country.Name,city.CountryCode,city.Population from city INNER JOIN country where country.CountryCode=city.CountryCode and city.population>5000 ORDER BY city.Population DESC";
This is working fine.

shuffle : Display only one row at the same time

How to display only one row at random at the same time from DB. Everything works fine, but all rows are displayed. thanks
<?php
$sql = "SELECT id,name FROM table ";
$rows = array();
$result = $objCon->query($sql);
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
shuffle($rows);
echo '<ol>';
foreach($rows as $row)
{
echo '<li><h3>'.$row['id'].' = '.$row['name'].'</h3></li>';
}
echo '</ol>';
?>
Change your SQL request:
SELECT id,name FROM table ORDER BY RAND() LIMIT 1;
You can do it using PHP:
....
shuffle($rows);
$randomRow = reset($rows);
....
But the better way is to change your SQL query:
$query = "SELECT id, name FROM table ORDER BY RAND() LIMIT 1;"
<?php
$sql = "
SELECT id, name
FROM table
ORDER BY RAND()
LIMIT 1 ";
$result = mysql_query($sql);
// As you are only return a single row you do you require the while()
$row = mysql_fetch_array($result);
echo '<ol>';
echo '<li><h3>'.$row['id'].' = '.$row['name'].'</h3></li>';
echo '</ol>';
?>
By adding an ORDER BY RAND() in your sql query you are asking MySQL to randomly order the results then at a LIMIT to restrict the number of rows you would like returned.
The example code is written based on selecting a single row. If you would like more, e.g. 5, you will need to add a while loop.

Displaying items from multiple tables on your webpage using php while loop

I am trying to dsiplay data from 2 different tables from my mysql database using a while loop.
Currently I can display the data from 1 table and limit the results to 3. I then want to display the first 5 records from another table. If I join the tables I can only display the same number of items from both using LIMIT?
I am using a while loop to display the content from a table called item, using the following code;
$query");
$result2 = #mysql_query($query, $connection)
or die ("Unable to perform query$query");
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php
}
?>
If I start another loop for the data from the next table called movie, however the data is not displayed using the following code;
<?php
while($row= mysql_fetch_array($result2))
{
?>
<?php echo $row['title'] ?>
<?php
}
?>
What is the best way to display the data from the 2 tables?
Many Thanks
I don't know if you forgot to paste a bit of code, but this should work:
<?php
$query = "select * from item order by date, time asc limit 0,3";
$result = mysql_query($query);
while($row= mysql_fetch_array($result))
{
echo $row['item'];
}
$query2 = "select * from movie limit 0,5";
$result2 = mysql_query($query2);
while($row= mysql_fetch_array($result2))
{
echo $row['movie'];
}
?>
You may be able to do it with one SQL Query too:
SELECT i.item, m.movie
FROM (SELECT * FROM item ORDER BY date, time ASC LIMIT 0,3) i,
(SELECT * FROM movie limit 0,5) m
Then in php:
<?php
while($row= mysql_fetch_array($result))
{
echo $row['item'];
echo $row['movie'];
}
?>
But that depends on how you want to format the output.

Categories