1

i'm currently on Apache 2.2 using PHP 5.. I am trying to connect to to MySQL on my Apache server and create a database called my_db... and i have the following code:

<?php
$con=mysqli_connect();
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "Connected Successfully!";
}
// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql)){
echo "Database my_db created successfully";
}else{
echo "Error creating database: " . mysqli_error();
}
?> 

However, when the script runs i get the following output:

Connected Successfully!Error creating database:

Please help. It is not an authentication problem since "Connected Successfully!" is printed...

Thankyou in advance!

3
  • maybe you haven't permission to create database on default user Commented Apr 10, 2013 at 16:05
  • I would suspect that the mysql user you are logging in with does not have CREATE rights. A mysql user can have one or a group of many different permissions. |Do you know if you have phpmyadmin? :phpmyadmin.net/home_page/index.php PHPmyadmin will allow you to administer mysql a lot easier. If you dont already have it, consider having it installed or using a tool such as Navicat. Commented Apr 10, 2013 at 16:05
  • To create and delete a database one should have admin privilege. Check it out Commented Apr 10, 2013 at 16:07

3 Answers 3

1

You can query an overview of the user privileges by using:

SHOW GRANTS FOR 'user'@'host';
Sign up to request clarification or add additional context in comments.

Comments

0

Try creating a new user on the mysql command line, grant this user all privileges, and then connect as this new user and try to create the database. This should solve your problem. Assuming you're connecting to mysql on localhost the code you need to create the new user is:

bash# mysql --user=root -p

mysql> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost' WITH GRANT OPTION;

Comments

0

Replace echo "Error creating database: " . mysqli_error(); with echo "Error creating database: " . mysqli_error($con);

Add $con as an argument in mysqli_error function, And you will get exact error,

http://php.net/mysqli_error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.