Background:
using LIGHTTPD as a Web server, PHP acts as a server script, MySQL as a database. When the browser client accesses, the server PHP script attempts to connect to MySQL, querying the data and returning it to the client. However, in the course of debugging occurs two times, unable to connect MySQL situation. Make a note here.
Body:
It's easier to connect to MySQL in a PHP script, just post the code.
<span style= "FONT-SIZE:14PX;" ><?php$servername = "192.168.110.2"; $port =3306; $username = "username"; $password = "password"; $dbname = "TestDB"; $socket = "Mysqli.default_socket";//Create Connection$conn = new Mysqli ($servername, $username, $password, $dbname, $ Port, $socket);//Check connectionif ($conn->connect_error) {die ("Connection failed:". $conn->connect_error);} $sql = "SELECT ID, FirstName, LastName from myguests"; $result = $conn->query ($sql); if ($result->num_rows > 0) {
//output data of each row while ($row = $result->fetch_assoc ()) { echo "ID:". $row ["id"]. "-Name:". $row ["FirstName"]. " " . $row ["LastName"]. "<br>"; }} else { echo "0 results";} $conn->close ();? ></span>
(1) The above code can be run
If you run the command line interface in a way that uses the PHP + script name, the code is able to print out the data for a table in MySQL.
(2) But when I accessed the script through a Web browser, I didn't get the result I wanted, and the code died in the MySQL connection.
Later analyzed the reason, should be the MySQL configuration parameter problem.
<span style= "FONT-SIZE:14PX;" > $server = "192.168.110.2";</span>
Here is the way to connect to MySQL remotely, but the default MySQL installation, only listen to 127.0.0.1 (that is, the native IP).
So if you want MySQL to be remotely accessible, you need to configure the IP of the MySQL listener.
Open the MySQL configuration file/etc/mysql/my.cnf and comment out the following line.
bind-address = 127.0.0.1
(3) After the comments, there is one more issue to note
That is, if you use the root user to remotely access MySQL, it will pop up a permissions issue (in MySQL native debugging will not appear, but the remote connection will appear).
Workaround: Create a normal user, and add the user to modify or query MySQL permissions, and then use this ordinary user to access MySQL.
PHP cannot connect to MySQL