0

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\n";


   $sql = "CREATE DATABASE TUTORIALS2";

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

     echo "Created the database\n";

   }

   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!

KexAri
  • 3,867
  • 6
  • 40
  • 80
  • 1
    `mysql_*` are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the `MySQLi` or `PDO_MySQL` extension should be used. – Dipen Shah Jul 13 '15 at 14:59
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 13 '15 at 15:02
  • 1
    edited my question for mysqli, still having the same issue – KexAri Jul 13 '15 at 15:16
  • Mysql_error, did that get converted too? – Drew Jul 13 '15 at 15:56
  • @DrewPierce sorry updated that. Still no luck. – KexAri Jul 13 '15 at 16:04
  • Look at what Alex answered. Once enabled look at logs or errors on devtest output – Drew Jul 13 '15 at 16:06
  • Wasn't that, has always connected fine. Just won't create the database now. – KexAri Jul 13 '15 at 16:09

1 Answers1

4

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

Community
  • 1
  • 1
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
  • not sure why I had the bad quotes, maybe because I copied and pasted from a command line editor. Fixed it though and it's not that. It always connects fine, just won't create a database – KexAri Jul 13 '15 at 16:09
  • AH! That helped me find the error! turns out the database wasn't connecting at all. I had "localhost:3036" just changed it to "localhost" and now everything works! thanks! – KexAri Jul 13 '15 at 17:20