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'];
}
}
Related
Im sorry for my bad english but this is the best i can.
I am trying to make a script that get's the item values of one line in my
for this example i'll be using the row with ID 2.
The first query is working correctly and getting the 2 values of 1items and 2items and deletes the ; that comes with it.
But the second part seems a little bit harder and i can't solve it.
The query is getting id, base_item from the table
The last 2 Outputs marked red are the one's that are matching 1items & 2items
I'm trying to let the if statement filter the $item1 as id in the item table and output the baseitem of the found row
same goes for item2
I'm using MySQL & MySQLi because this cms doesnt support newer versions of PHP yet.
<?php
$query = "SELECT * FROM logs_client_trade ORDER by id DESC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['1id'] . "</td>");
echo("<td>" . $row['2id'] . "</td>");
$userinfo = "SELECT id, base_item FROM items LIMIT 1";
$result2 = mysql_query($userinfo) or die(mysql_error());
while($row2 = mysql_fetch_assoc($result2)){
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
}
$tradetime = $row['timestamp'];
$date = date("d M Y - H:i:s", $tradetime);
echo("<td>$date</td></tr>");
}
?>
You forgot the while loop for the second query:
$userinfo = mysql_query("SELECT id, base_item FROM items");
while($get2 = $userinfo->fetch_assoc()) {
$baseid = $get2['id'];
$baseitem = $get2['baseitem'];
echo("<tr>");
[...]
}
In addition to that, your code is a mess. In the second loop you are still listing the rows from the logs table... I suggest you to review carefully your code, being careful on how you are using the different $row and $get2 arrays.
You can use a JOIN to get all of the info with one SQL statement:
SELECT *
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
UPDATE:
Here's the PHP code that will run the query and output the table rows. I've specified the column names based on the code in the original question.
<?php
$query = "SELECT
a.`id`,
a.`1id`,
a.`2id`,
a.`1items`,
a.`1items`,
IFNULL(b.`baseitem`,'Not Available') as `baseitem1`,
IFNULL(c.`baseitem`,'Not Available') as `baseitem2`,
DATE_FORMAT(a.`timestamp`,'%d %m %Y - %H:%i:%s') as `tradetime`
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo "<tr>\r\n";
echo " <td>" . $row['id'] . "</td>\r\n";
echo " <td>" . $row['1id'] . "</td>\r\n";
echo " <td>" . $row['2id'] . "</td>\r\n";
echo " <td>" . $row['baseitem1'] . "</td>\r\n";
echo " <td>" . $row['baseitem2'] . "</td>\r\n";
echo " <td>" . $row['tradetime'] . "</td>\r\n";
echo("</tr>\r\n");
}
?>
I am trying to find if two runners have ever ran in the same race. The two runners are Peter Smith and Diane Peters.
$resultRaceType = mysqli_query($db,"SELECT DISTINCT date,time FROM results where runner = 'Peter, Smith' ");
while($row = mysqli_fetch_array( $resultRaceType ))
{
$resultRaceType1 = mysqli_query($db,"SELECT * FROM results where date = ' " . $row['date'] . " ' and time = ' " . $row['time'] . " ' and runner = 'Diane, Peters'");
while($row1 = mysqli_fetch_array( $resultRaceType1 ))
{
echo "<tr >";
echo "<td>";
echo $row1['date'];
echo " - " . $row1['time'];
echo "</td>";
echo "<tr>";
}
}
The above code works, but only if I limit the first select to LIMIT 50. So I can see that it is timing out. My table has over 100K rows. I know I am doing something wrong but cant see what it is.
Thanks for any help you guy's can give me.
Try:
SELECT a.date, a.time FROM results a
JOIN results b ON (a.date = b.date AND a.time = b.time)
WHERE a.runner='Peter, Smith' AND b.runner='Diane, Peters';
What is wrong about this code??
echo "<table>";
echo "<tr><td>Machine Nummer</td><td>Eigenaar</td><td>Status</td><td>Locatie</td></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['machineid'] . "</td><td> ". $eigenaar=mysql_query("SELECT userName FROM users WHERE userId =" . $row['eigenaar'] ."") ."</td><td>" . $row['status'] . "</td><td>" . $row['locatie'] . "</td></tr>";
}
echo "</table>";
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$query = "SELECT * FROM machines WHERE locatie='".$_SESSION['location']."'"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
I want it to output the eigenaar name which equals to the userId but i only get the output:Resource id #9 Resource id #10 Resource id #11.
The answer is to use the INNER JOIN function.
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...
I read somewhere (sorry I forgot where), that there is a way to associate an index number to rows fetched from an MySQL query result using a native PHP function (the person was not sure about the syntax so he didn't wrote it). For example:
The usual way:
$count = 1;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<li> ". $count ."id: " . $row['id'] . " lon: " . $row['lon']. ", lat: ". $row['lat'].", ". $row['road']. " </li>";
$count++;
}
I know that thinking of this other way is not that important, but I'm just curious if there really is a function for that purpose.
you can add rownum to your results array, if you edit your sql like
SELECT #rownum:=#rownum+1 as rownum, p.* FROM MYTABLE p , (SELECT #rownum:=0) r
so the loop like
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<li> ". $row['rownum'] ."id: " . $row['id'] . " lon: " . $row['lon']. ",
lat: ". $row['lat'].", ". $row['road']. " </li>";
}