我是php的新手并尝试制作登录表单,但是在建立连接时它们会出错。我没有得到它。
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'simple_login');
$conn = mysqli_connect(DBHOST,DBUSER,DBPASS);
$dbcon = mysqli_select_db(DBNAME,$conn);
if ( !$conn ) {
die("Connection failed : " . mysqli_error());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysqli_error());
}
如果有人知道帮助我,请提前。谢谢。
答案 0 :(得分:2)
需要将第一个参数作为连接传递给您的数据库名称
mysqli_select_db ($conn, DBNAME );
阅读http://php.net/manual/en/mysqli.select-db.php
将连接错误检查为
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
答案 1 :(得分:1)
试试以下代码:
选择数据库时,您必须先传递$conn
,您已更改参数顺序。
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'simple_login');
$conn = mysqli_connect(DBHOST,DBUSER,DBPASS);
$dbcon = mysqli_select_db($conn,DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysqli_connect_errno());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysqli_connect_errno());
}
希望这有帮助!!