没有数据库选择mysqli消息


No database selected mysqli message

我将项目中的mySQL代码更改为mysqli,但现在我收到消息"未选择数据库",尽管所有数据库信息都在这里是代码:

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');  // Your database Username
define('DB_PASSWORD', 'root'); // Your database Password
define('DB_DATABASE', 'social'); // Your database Name    
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysqli_error());
$database = mysqli_select_db($connection,DB_DATABASE) or die(mysqli_error());
mysqli_query($connection,$database);
mysqli_query($connection,"SET NAMES 'UTF8'");
mysqli_query($connection,"SET character_set_connection = 'UTF8'");
mysqli_query($connection,"SET character_set_client = 'UTF8'");
mysqli_query($connection,"SET character_set_results = 'UTF8'");  
$path = "uploads/";
$LogoPaht="css/icons/";
$conversation_uploads = "conversation_images/";
$profile_image_path = "user_profile_uploads/";
$admin_path = "../".$LogoPaht;
$admin_profile_path='../'.$profile_image_path;
$admin_path_uploads='../'.$path;
$profile_cover_pic_path = "user_profile_cover_uploads/"; // User Profile Cover File
$perpage=10; // Updates perpage
$base_url='http://localhost/sociall/'; // base_url
$admin_base_url=$base_url.'smadmin/'; // Admin base_url
$gravatar=0; // 0 false 1 true gravatar image
$rowsPerPage=1000000; //friends list
$profilePerPage=3;
/*SMTP Details */
$smtpUsername='yourname@gmail.com'; //yourname@gmail.com or you can use your webmail like somename@yourwebsitename.com
$smtpPassword='pass';  //gmail password or your webmail password
$smtpHost='ssl://mail.yourwebsitename.com'; //tls://smtp.gmail.com if yo
$smtpPort='465'; //465
$smtpFrom='yourname@gmail.com'; //yourname@gmail.com}
?>

删除以下行:

$database = mysqli_select_db($connection,DB_DATABASE) or die(mysqli_error());
mysqli_query($connection,$database);
通过将

代码更改为以下内容,将数据库名称添加到连接建立请求中:

 define('DB_SERVER', 'localhost');
 define('DB_USERNAME', 'root');  // Your database Username
 define('DB_PASSWORD', 'root'); // Your database Password
 define('DB_DATABASE', 'social'); // Your database Name    
 // specify the database name when establishing the connection
 $connection = 
    mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
 /***
    If your code dies before this comment, somethings wrong with 
    your credentials, the db name, or the db's not running on the host 
    you're trying to connect to
 ***/

 // should be able to query away if connection succeeded...
 mysqli_query($connection,"SET NAMES 'UTF8'");
   ...
   ...

如果您的连接失败,可能是因为您的 mysql 设置为端口访问而不是套接字访问。 更改要测试的数据库行:

 define('DB_SERVER', '127.0.0.1');

如果此操作失败,您确定您使用的是套接字,请将其保留为localhost并验证您的用户是否具有 mysql 数据库的本地主机权限。