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);
Related
This question already has answers here:
PHP PDO retrieves duplicate data
(2 answers)
Closed 1 year ago.
I'm newbie in PHP and when I use PHP to query select, I got 2 values, I realize it may be make slow if have 2 duplicated values.
So I want to ask about it and I also want to get just 1 values from select
thanks for reading my question.
here is picture:
It dipends on what your i guess sql query respondes to your query. However if you want cut down your result in sql you can use LIMIT 1
SELECT ...
FROM ...
WHERE ...
LIMIT 1
This question already has answers here:
Inserting multiple rows in mysql
(5 answers)
Closed 5 years ago.
I have a PHP script and I'm inserting values into a MySQL table. This was working fine when dealing with a few thousand lines of data but as I increase the data only a part of the data is inserted into the MySQL table.
It seems to stop after only about 6000 rows of data. REally I want it to work for 40,000 lines and later it needs 160,000 line.
I have to run the script several times to get more data added into the table.
I am new to working with SQL statements and I don't think the way I have set it up is efficient.
Some of my code:
for($x=0;$x<count($array_athlete); $x++){
$checkuser=mysqli_query($link,"SELECT * FROM `Events_testing2` WHERE `location`='$location'
AND `barcode`='$array_barcode[$x]' AND `date`='$date'");
$rowcount=mysqli_num_rows($checkuser); //checks if barcode exists for that user in that location on that date. Inserts data if doesn't already exist.
if($rowcount>0){
}
else{
$queryInsertUser=mysqli_query($link, "INSERT INTO `Events_testing2` (`eventID`,`location`,`date`,`barcode`,`athlete`,`time`,`Run Points`,`Volunteer Points`,`Gender`,`Gender pos`)
VALUES (' ','$location','$date','$array_barcode[$x]','$array_athlete[$x]','$array_time[$x]','$array_score[$x]',' ','$array_gender[$x]','$array_gender_pos[$x]') ");
}
}
Any advice of how to insert more rows quickly into the database would be appreciated.
Many thanks
Insert by chunk, like insert first 1000 (1 - 1000) then next 1000 (1001 - 2000). In this way, you will not encounter errors.
Try to set the time_limit of php beforehand
<?php
ini_set('max_execution_time', '0'); // infinite
set_time_limit(0); // infinite
/// do your stuff
See:
http://php.net/manual/en/info.configuration.php#ini.max-execution-time
http://php.net/manual/en/function.set-time-limit.php
This question already has answers here:
MySQL pagination without double-querying?
(9 answers)
Closed 7 years ago.
As I know a pagination structure requires minimum two sql query.
First, find total row
Second, limit your query.
Is there a way to decrease query to one. Can we use first sql query to manupulate the pagination? On first query we already fetch all necessary data. Is first query's array can handle this issue?
You can do a simple previous/next pagination with a single query.
For this i your result limit is say 25, just query for 26 and only display the 25. If you get back less than 26 results, you know you don't have any more.
However if you are wanting to accurately display links for page 1,2,3,etc.. you have to do both a query for the total number of records in the table, and a query for just the data you want to display.
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.