doulingzou1712
2015-07-13 14:54
采纳率: 0%
浏览 42
已采纳

无法在php中创建新的MySQL数据库

I am having issues trying to create a new MySQL database in php,

my code is:

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = ‘somepassword’;

   $conn = new mysqli($dbhost, $dbuser, $dbpass);
   if(!$conn->connect_error )
   {
     die('Could not connect: %s' . $conn->connect_error);
   }
   echo "Connected successfully
";


   $sql = "CREATE DATABASE TUTORIALS2";

   if($conn->query($sql)===TRUE){

     echo "Created the database
";

   }

   else {
     echo "Failed to create the database".$conn->error;
   }



   //Close the database
   $conn->close();


 ?>

MySQL connects fine but it won't allow me to create a new database. Not a clue what I'm doing wrong here. Any pointers would be greatly appreciated!

图片转代码服务由CSDN问答提供 功能建议

我在尝试在php中创建新的MySQL数据库时遇到问题,

我的代码是:

 &lt;?php 
 $ dbhost ='localhost:3036'; 
 $ dbuser ='root'; 
 $ dbpass ='somepassword  '; 
 
 $ conn = new mysqli($ dbhost,$ dbuser,$ dbpass); 
 if(!$ conn-&gt; connect_error)
 {
 die('无法连接:%s')。  $ conn-&gt; connect_error); 
} 
 echo“已成功连接
”; 
 
 
 $ sql =“CREATE DATABASE TUTORIALS2”; 
 
 if($ conn-&gt; query($  sql)=== TRUE){
 
 echo“创建数据库
”; 
 
} 
 
否则{
 echo“无法创建数据库”。$ conn-&gt;错误;  
 
 
 
 
 //关闭数据库
 $ conn-&gt; close(); 
 
 
?&gt; 
   
 
  

MySQL连接正常,但它不允许我创建一个新的数据库。 不知道我在这里做错了什么。 任何指针都将非常感谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumei1926 2015-07-13 14:57
    已采纳

    Second Edit: I somehow missed this, apologies.

    You have a mistake here:

    if(!$conn->connect_error )

    should be

    if($conn->connect_error )

    see here http://php.net/manual/en/mysqli.connect-error.php


    Edit:

    run this query SHOW GRANTS FOR 'root'@'localhost'; either from php or from PhpMyAdmin or similar and see if the user root has the privilege to create databases.

    More here http://dev.mysql.com/doc/refman/5.0/en/show-grants.html


    First of all you need to enable error_reporting and second you have some bad quotes on this line $dbpass = ‘somepassword’;

    replace like this

    $dbpass = 'somepassword';

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题