This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I have the following:
<?php
include 'connect.php';
//get average from all reviews
$allreviews = "SELECT round(avg(Stars_overall),0) AS average FROM (SELECT Stars_overall FROM Reviews WHERE Meal_ID = 1 ORDER BY Order_ID DESC LIMIT 5) AS Average";
$getresult = mysqli_query($conn, $allreviews);
$row3 = mysql_fetch_assoc($getresult);
echo "aaaaa" .$row3['average']. "bbbbb";
mysqli_close($conn);
?>
Unfortunately it is not working, any clue why? It is for a review system, i'm trying to display an average of star ratings stored in database. The first sql gets all the ratings for a specific meal and the second sql calculates the average. I then want to be able to display the average
You are using mysqli, therefore you must use mysqli_fetch_assoc and not mysql_fetch_assoc.
Related
This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed last month.
I have a database with 3 million records. I want it to randomly fetch 10 records. I read somewhere that you can randomly select records from a range of 1000 records so that it does not search the entire database.
I am currently using the following code which is very heavy.
$result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM miasta");
$offset_row = mysqli_fetch_object($result);
$offset = $offset_row->offset;
$result = mysqli_query($conn, "select * from (SELECT * FROM miasta) as podseld ORDER BY RAND() LIMIT $offset, 10");
$row = mysqli_fetch_array($result)
Please help me to speed up this query. Thank you in advance.
Randomly selected records take a long time to upload.
With updated statistics, you should be pretty quick at getting the total row count. So, avoid an ORDER BY.
Try:
SELECT
*
FROM miasta
WHERE RAND() <=
10 / (SELECT COUNT(*) FROM miasta)
LIMIT 10
As we use RAND(), here, you might get a higher number than 10 rows, so LIMIT 10. And, sometimes, you might get fewer than 10 rows (it's random), but then just re-run it until you have 10 rows.
This question already has answers here:
Simple PHP Pagination script [closed]
(3 answers)
Closed 6 years ago.
A mysql table contain 100 rows. I am trying to display first 20 rows with 5 rows per page.
I think query should be like this
SELECT * FROM `table` top 20 LIMIT 0, 5
but how can i use this concept. waiting for your help.
You can use LIMIT and OFFSET controls, LIMIT is for max rows you want to show and OFFSET is the starting position:
SELECT * FROM db_name ORDER BY db_table LIMIT 5 OFFSET 0
This question already has answers here:
MySQL: Alternatives to ORDER BY RAND()
(9 answers)
Closed 6 years ago.
I have a MySQL database with 112 rows (with ID column from 1 to 112), I need to SELECT 6 random rows (doesn't matter if sequential) to show in PHP/HTML page and to be changed daily.
The only option I think is to depend on the current date.
Is there any solution ?!
Thank you ...
EDIT:
Question was solved. Although no one really understood what I want but I keep getting down votes.
The question was : 6 Random rows EVERY DAY not every page refresh or every call from DB.
But thanks for the amazing effort. Question solved.
The MySQL statement ORDER BY RAND() will order the matching rows randomly. Combined with LIMIT 6 you'll get six random rows.
See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand for reference.
Just use ORDER BY RAND() to randomize the row order, then show the first 6:
SELECT * FROM Yourtable ORDER BY RAND() LIMIT 0,6;
Use following query
SELECT column FROM table
ORDER BY RAND()
LIMIT 6
This question already has answers here:
Read one column from one row from a MySQL database
(8 answers)
Closed 8 years ago.
I am having an issue declaring a PHP variable as a MySQL result.
Code:
$num = mysql_query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
What it does it retrieve the latest entry in the database, which is an int, and I want it to store into the PHP variable $num. Is this possible?
Is this possible?
Yes, that is possible.
You should, however, use mysqli or PDO instead of the old mysql_ functions.
See: Why shouldn't I use mysql_* functions in PHP?
An example of how it could look:
$result = $mysqli->query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
$num = $result->fetch_object()->num;
This question already has answers here:
Anyway to Limit MySQL Query Execution time?
(4 answers)
Closed 9 years ago.
How can I limit the runtime of a SQL I call with a php file to 2 seconds
here an example I am using
$output = '';
$sql = "SELECT epf_application.view_url FROM epf_application INNER JOIN app_uri ON epf_application.application_id=app_uri.application_id WHERE app_uri.uri = '$URL' LIMIT 1";
foreach ($xpdo->query($sql) as $row) {
$output .= $row['view_url'];
}
I dont want to limit the whole script runtime only the request on the database
You can't! As the query is blocking and you'll only know how long it took when it's done.
So you need to optimize your Queries if they are too slow. You do that by analyzing and testing and by using INDEXES in the right places and playing with EXPLAIN.