PHP with MySQL is Slow

╄→гoц情女王★ 提交于 2019-11-28 09:13:29
Wap

Answer: 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-500ms.

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

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');

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!