简体   繁体   English

使用PHP代码的MySQL连接错误

[英]mySQL connection error using php codes

"Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond. in C:\\xampp\\htdocs\\appraisal\\database.php on line 5 No database selected" “警告:mysql_connect():连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接主机未能响应而建立连接失败。位于C:\\ xampp \\ htdocs \\ appraisal \\ database.php中第5行:未选择数据库”

Why does it show most of the time? 为什么大多数时间都显示? But when I refresh the page by entering F5, the error will be gone and will show the output correctly. 但是,当我通过输入F5刷新页面时,错误将消失并且将正确显示输出。 This doesn't really affects the system but it is quite bothering when our users see the error. 这并不会真正影响系统,但是当我们的用户看到错误时,它会很麻烦。

I have the codes here. 我在这里有代码。

<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal');
?>

Hoping for your response. 希望您的回应。 Thanks! 谢谢!

Try adding the $conn parameter to the mysql_select_db function call 尝试将$conn参数添加到mysql_select_db函数调用中

<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('db_appraisal', $conn);
?>

Link to The Manual 链接到手册

If you are writing new PHP code you should really not be using the mysql_* extension as it has been deprecated ie it will be removed from PHP sometime soon. 如果您正在编写新的PHP代码,则实际上不应该使用mysql_*扩展名,因为它已被弃用,即它将很快从PHP中删除。

Use either the mysqli_* extension or the PDO one. 使用mysqli_*扩展名或PDO一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM