i'm trying to find a solution to make my php script show only the last 3 entries in a row but all i can find is how to show the first entries not the last 3. any ideas?
this is the script that shows the entries:
<?php
// Grab the data from our people table
$sql = "select * from people";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" /><br />";
echo $row['fname'] . " " . "<br />" . "<br />" . $row['lname'] . "<br />";
echo "</p>";
echo "</div>";
}
?>
try this
$sql = "SELECT * FROM people ORDER BY id DESC LIMIT 3";
id=>is your column name you you might have to change this
and dont use mysql function as they are depricated and soon going to be dropped. Learn mysqli or PDO
Just do reverse ORDER BY, ie. if your table is ordered by column id, then do ORDER BY id DESC and then use LIMIT 3:
$sql = "select * from people ORDER BY id DESC LIMIT 3";
And PS, mysql_* are deprecated...
Related
I want to echo an img every time $i increases, but it only echos the last $i.
for($i=0; $i<=5;$i++)
$sql="SELECT DISTINCT id, Make FROM sve WHERE id='".$i."'";
$rezult=mysqli_query($db, $sql);
$red=mysqli_fetch_object($rezult);
echo "<img src='brend/brend$red->id.png' class='imgbrend' strana='".$red->Make."'/>";
You can use MySQL's IN or BETWEEN keywords, rather than using a for loop and then iterate over the result set.
Your query becomes something like this:
SELECT DISTINCT id, Make FROM sve WHERE id BETWEEN 0 AND 5
Then you need a loop like so:
while ($red = mysqli_fetch_object($rezult)) {
echo "<img src='brend/brend" . $red->id . ".png' class='imgbrend' strana='" . $red->Make . "'/>";
}
Full code:
$sql = "SELECT DISTINCT id, Make FROM sve WHERE id BETWEEN 0 AND 5";
$rezult = mysqli_query($db, $sql);
while ($red = mysqli_fetch_object($rezult)) {
echo "<img src='brend/brend" . $red->id . ".png' class='imgbrend' strana='" . $red->Make . "'/>";
}
I want to select faster than this my page load is very slow when executing the script.
$bmw= mysqli_query($con,"SELECT * FROM cars WHERE name='bwm'
ORDER BY date DESC LIMIT 1"); $mercedes= mysqli_query($con,"SELECT * FROM cars
WHERE name='mercedes' ORDER BY date DESC LIMIT 1");
$audi= mysqli_query($con,"SELECT * FROM cars WHERE name='audi'
ORDER BY date DESC LIMIT 1");
$skoda= mysqli_query($con,"SELECT * FROM cars WHERE name='skoda'
ORDER BY date DESC LIMIT 1");
And echo it out like here below, like that the page load is very slow.
while($row = mysqli_fetch_array($bmw)) { echo $row['name'] . " " .
$row['date'];
}
while($row = mysqli_fetch_array($mercedes)) { echo $row['name'] . " "
. $row['date'];
}
while($row = mysqli_fetch_array($audi)) { echo $row['name'] . " " .
$row['date'];
}
Thanks in advance
There is nothing wrong in the queries you wrote, your performance issue may be due to :
missing indexes on name and date : create indexes on name and date
cars database too big for your hardware : new hardware/partitionning/redesign
network link slow : solution provided by Lacy K is interesting as there is only one query sent to mysql
You could also improves performance a bit by selecting only name and date instead of *
select name, date from ...
Why not selecting all make at once like this:
$q= mysqli_query($con,"SELECT * FROM cars WHERE name IN ('bwm', 'mercedes', 'audi', 'skoda') ORDER BY date DESC LIMIT 1");
while($row = mysqli_fetch_array($q)) {
if($row['name'] == 'bmw'){
echo $row['name'] . " " . $row['date'];
}
elsif($row['name'] == 'mercedes'){
echo $row['name'] . " " . $row['date'];
}
elsif($row['name'] == 'audi'){
echo $row['name'] . " " . $row['date'];
}
elsif($row['name'] == 'skoda'){
echo $row['name'] . " " . $row['date'];
}
else{
echo $row['name'] . " " . $row['date'];
}
}
<?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 am trying to perform an additional query as you can see from the below piece of code.
I am having issues performing the second query. It seems like the code is not recognizing the second query. Can someone please advice?
Thanks in advance.
$query = "SELECT * FROM tst WHERE status ='active' order by created DESC LIMIT 9";
$result = mysql_query($query);
if (!$result) die ("database access failed : " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo"<ul class='gallery'><li>";
echo "<td>" . $row['adid'] . "</td>";
$subquery = ("SELECT * FROM match_item_image where adid = '.$row['adid'].' LIMIT 1 ");
$results = mysql_query($subquery) or die ("Error in query: $subquery " . mysql_error());
$rows = mysql_fetch_array($results);
$rows = mysql_num_rows($results);
echo ' <img src= "upload/'.$rows['image'].'"height="175" width="200"border="0" /> ';
echo "<h2><a href=''>".$row['state'] . "</a></h2>";
echo "<h2><a href=''>".$row['loc'] . "</a></h2>";
echo"
</li>
</ul>";
}
the query is not working because you have included the $row['adid'] as a string and not as a value in the query, concactenate it like this
"SELECT * FROM match_item_image where adid = '" . $row['adid'] . "' LIMIT 1 "
and your query is now vulnerable with SQL Injection, please read the article below
Best way to prevent SQL injection in PHP
$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.