Query Time Result in MySQL W/ PHP

Query time result in MySQL w/ PHP

$starttime = microtime(true);

//Do your query and stuff here

$endtime = microtime(true);
$duration = $endtime - $starttime; //calculates total time taken

NOTE that this will give you the run time in seconds(not microseconds) to the nearest microsecond due to get_as_float parameter being true. See this

How do I display execution time of a MySQL query in PHP?

This worked like a charm!

    $db->query('set profiling=1'); //optional if profiling is already enabled
$db->query($_POST['query']);
$stmt = $db->query('show profiles');
$db->query('set profiling=0'); //optional as well

$records = $stmt->fetchAll(PDO::FETCH_ASSOC);

$errmsg = $stmt->errorInfo()[2]; //Output the error message

UPDATE (The following now works on innodb on my present setup)

$db->query('set profiling=1'); //optional if profiling is already enabled
$db->query($_POST['query']);
$res = $db->query('show profiles');
$records = $res->fetchAll(PDO::FETCH_ASSOC);
$duration = $records[0]['Duration']; // get the first record [0] and the Duration column ['Duration'] from the first record

Result of (show profiles) from phpmyadmin.

Query_ID    Duration    Query   
1 0.00010575 SELECT DATABASE()

Getting the actual (absolute) execution time of the last query in PHP (excluding network latency etc)

how to check mysql query execution time in whole code

try this code to get a execution time of sql query

$sql = '...';
$msc = microtime(true);
mysql_query($sql);
$msc = microtime(true)-$msc;
echo $msc . ' s'; // in seconds
echo ($msc * 1000) . ' ms'; // in millseconds

Hope this one worked for you.

Calculating Time of a query php & Mysql

Your microtime function is not necessary, simply do

$start = microtime(TRUE);
... do query
$end = microtime(TRUE);

passing in the TRUE value has microtime() return the timestamp as an actual float, not that moronic string format that some [truly nasty but accurate description of said person's intelligence] thought would be a useful way of returning the data.

Long query time in PHP-MYSQL SELECT

Your query is running long because it is not using indexes because of the wildcard and LIKE comparison.

LIKE "%$query%"

Read more here: http://dev.mysql.com/doc/refman/5.6/en/index-btree-hash.html

If it is acceptable you may change your query to

LIKE "$query%"

Although this will produce different results it will (at least it should) create a much quicker query.

Wildcards are far from ideal!

Display query result with PHP and MySQL

You're doing a good job for that you just started with PHP. Anyway, there's a little mistake in your code.

You actually query the database, but you don't fetch the result.

You have to do the following after your query:

$row = $result->fetch_row();
print $row[0]; // $row[0] will contain the value you're looking for

Also it seems that your $entity is not equal to 0. I don't see you initializing this variable anywhere, are you sure you have defined it? May you want to show us some mor of your code..

MYSQL compare date time not returning any results

You're missing an $stmnt->execute()

Note that you can replace ->prepare with ->query if you wish to run the query immediately rather than preparing it first.

More info: PDO's query vs execute



Related Topics



Leave a reply



Submit