Use php to access the local mysql database. the code is as follows: {code...} when the host name is localhost, the records in the database table can be displayed normally. When the host name is written as 127.0.0.1, QUERYERROR is reported even though no error is reported when the database is connected. In this case, localhost... uses php to access the local mysql database. the code is as follows:
$mysqli = new mysqli("localhost", $name, $passwd, "test");if ($mysqli->connect_errno) { die("Failed to Connect to mysql: " . $mysqli->connect_errno);}$result = $mysqli->query("SELECT * FROM pet;");if(!$result){ echo "QUERY ERROR: " . $mysqli->error;}... ...$mysqli->close();
When the host name is writtenlocalhost
The records in the database table can be displayed normally. When the host name is written127.0.0.1
Even if no error is reported when you connect to the databaseQUERY ERROR
. So Farlocalhost
And127.0.0.1
Are they different? What's going on?
Reply content:
Use php to access the local mysql database. the code is as follows:
$mysqli = new mysqli("localhost", $name, $passwd, "test");if ($mysqli->connect_errno) { die("Failed to Connect to mysql: " . $mysqli->connect_errno);}$result = $mysqli->query("SELECT * FROM pet;");if(!$result){ echo "QUERY ERROR: " . $mysqli->error;}... ...$mysqli->close();
When the host name is writtenlocalhost
The records in the database table can be displayed normally. When the host name is written127.0.0.1
Even if no error is reported when you connect to the databaseQUERY ERROR
. So Farlocalhost
And127.0.0.1
Are they different? What's going on?
Loaclhost and 127.0.0.1 cannot be equivalent.
Loaclhost isNameThis name generally represents 127.0.0.1IP addressThat is to say, when you say to the computer that I want to go to loaclhost, the computer will translate it into 127.0.0.1 through the DNS, NBNS, or hosts file.
If you use loaclhost to connect to the database, Mysql uses unixsocket to link. If this parameter is set to 127.0.0.1, TCP/IP is used for the link.
Therefore, try to use 127.0.0.1.
Well, I can see that the answer has been adopted, but I 'd like to say a little more.
In fact, the answer is not very rigorous, but since it has been adopted, let's say a little more. Generallylocalhost
And127.0.0.1
It is equivalent, but mysql has some differences in terms of processing this term. it uses different communication methods based on different addresses.
The reason is, I guess it is probably for local applications to achieve better performance. Andlocalhost
This address does not match in mysql. That isuser@'%'
Cannot matchuser@'localhost'