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

无法在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!

  • 写回答

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';

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看