(IMPORTANT) EDIT 3: Running the testajax2.php by itself and not Ajax. The duration is about the same, 1.02-1.03 seconds. So I guess that means the
The problem is the localhost lookup from localhost to 127.0.0.1.
The solution is to either add the line 127.0.0.1 localhost
to your windows hosts file as @Wap has answered (and thank you for you answer!)
Or if you do not have access to the hosts file, you can simply change localhost to 127.0.0.1 in your php mysqli call:
$mysqli = new mysqli('127.0.0.1'), 'root', '', 'testdb');
It could be happening in a couple of places: 1) the query itself, or 2) the creation of the mysqli instance (and resulting db connection). If I had to guess, I suspect your problem is (2).
One quick-and-dirty debugging method is to use microtime()
.
http://www.php.net/manual/en/function.microtime.php
Example:
$start = microtime(TRUE); // Start counting
$mysqli = new mysqli('localhost', 'root', '', 'testdb');
$temp = microtime(TRUE) - $start;
echo "Time to connect: {$temp}"; // Check elapsed time
$mysqli->set_charset("utf8");
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$temp = microtime(TRUE) - $start;
echo "Time to setup: {$temp}"; // Check elapsed time
$testreceive = $_POST['test_id'];
$query = "SELECT First_Name from tblperson";
$result = $mysqli->query($query);
$temp = microtime(TRUE) - $start;
echo "Time to query: {$temp}"; // Check elapsed time
$row = $result->fetch_all(MYSQLI_ASSOC);
$temp = microtime(TRUE) - $start;
echo "Time to fetch {$temp}"; // Check elapsed time
Add the following line in the hosts file located in ”C:\Windows\System32\drivers\etc”
127.0.0.1 localhost
This solved my problem. PHP-MySQL can now complete its task depending on how complex somewhere between 20-500 ms.