【问题标题】:Warning: mysqli_select_db() expects parameter 1 to be mysqli [duplicate]警告:mysqli_select_db() 期望参数 1 为 mysqli [重复]
【发布时间】:2017-04-19 06:13:52
【问题描述】:

我是 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());
 }

如果有人知道请帮助我。提前谢谢。

【问题讨论】:

  • 在每行之后回显并在失败之​​后进行注释。
  • 下面有两个人回答了。

标签: php mysqli


【解决方案1】:

需要将第一个参数作为连接传递,然后是您的数据库名称

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();
        }

【讨论】:

    【解决方案2】:

    试试下面的代码:

    选择DB的时候要先传$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());
     }
    

    希望有帮助!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多