Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

php - mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'domain name' (111)

Im very new to php and my SQL, and am having trouble connecting to my SQL Database. Im getting the following error when i submit my HTML Form.

mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'domain name' (111)

Here is my php code:

<? 

 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $location=$_POST['location']; 
 mysqli_connect("domain_name", "1645347_data", "*password omitted*") or die(mysql_error()); //error getting thrown at this line
 mysqli_select_db("1645347_data") or die(mysql_error()); 
 mysqli_query("INSERT INTO `data` VALUES ('$name', '$email', '$location')"); 
 Print "Your information has been successfully added to the database."; 
 ?>

As you can see, it is a very simple php script (as I am following a tutorial).

Can anybody see why i would be getting this error? im more than happy to provide additional information if requested.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like your script is trying to connect "domain_name", but it can't find that server because it's not listed on the DNS's. Is your server on the same machine? In that cause you need to replace domain_name with localhost, 127.0.0.1 or the ip of the server.

ALSO MAKE SURE TO SANITIZE YOUR INPUTS BEFORE YOU RUN THE SQL COMMANDS!!!!

This should help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...