没有数据库选择mysqli消息

时间:2016-03-03 17:09:00

标签: php mysqli

我将项目中的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}
?>

1 个答案:

答案 0 :(得分:0)

删除行:

$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设置为端口访问而不是套接字访问。将db行更改为test:

 define('DB_SERVER', '127.0.0.1');

如果此操作失败,您确定使用的是套接字,请将其保留到localhost并验证您是否具有对mysql db的localhost权限。