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.
Related
This question already has answers here:
LIMIT 10..20 in SQL Server
(15 answers)
Closed 3 years ago.
hello guys i try data use datatables from serverside (SQL Server) if me use code
my data not found or not availaible. this is my code
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
please help me
Use OFFSET and FETCH in SQL Server OFFSET FETCH Clause.
SELECT * FROM table ORDER BY column OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;
Possible duplicate: LIMIT 10..20 in SQL Server.
This question already has answers here:
How do I calculate the offsets for pagination?
(4 answers)
How to apply bindValue method in LIMIT clause?
(11 answers)
Closed 5 years ago.
I asked a question yesterday titled "How do i use binded variables with MySql LIMIT clause. Sorry, I guess it was not clear enough and got 30 views but no suggestions. So here is what I found after a day of trial and error. First, I discovered that the the LIMIT is the actual physical position of a record in a table not my auto_incremented id_ number. A lot of documentation i read also indicated it was the OFFSET that held the record position, so I wasted a lot of time with weird results. Second, I found documentation with a couple of ways to bind a variable to LIMIT. None of them worked, so I resorted to assigning a $variable to the LIMIT as follows.
("SELECT * FROM $livetable WHERE cust_zip = :cust_zip
HAVING distance < :mydistance ORDER BY client_id ASC LIMIT $start_record_position
, $how_many_records_to_display");
This works for me but any better suggestions, would be appreciated. And I hope this will save someone else a day of figuring this out.
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.
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:
How to show the last queries executed on MySQL?
(10 answers)
Closed 8 years ago.
Is there a way to see the previous query entered in a MySQL server through PHP? I do not want the results of the query, I want the actual plaintext query entered.
No. By default PHP disconnects and closes any mysql connections when the script ends, so there will be nothing left to view when the NEXT connection comes in. And since you have to send the query over each time anyways, by definition you should have that query available to you already. If you need the query text kept, then you should put it into a variable first
e.g. instead of
$result = mysql_query('SELECT ...');
you should be doing
$sql = 'SELECT ...';
$result = mysql_query($sql);