Can't create a MySQL database with PHP on server?
I keep getting this error, and I'm not really sure why t开发者_运维技巧his isn't working here.
I'm trying to create a database with PHP and it's returning the following error:
Error creating database: Access denied for user 'scriptcooke'@'10.%' to database 'my_db'
I believe I have the correct credentials, but what does @10.% even mean?
NOTE: I'm trying to use the w3schools code example here:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
@10.%
is the field for the source host in the user definition.
The problem is: You don't have the permissions to create a new database. The user you are using may only use his own database;
The 10. Refers to the subnet's IP's first octet. In English, it's referring to the network "portion" that the user is connecting from. Try @localhost or @.*
<?php
//Create simple connection with no any error.
//connect with no password but.
$con=mysql_connect("localhost","root","");
mysql_select_db("my_db") or die(mysql_error());
//Retrieve all the data from the "example" table
$result = mysql_query("select * from contact LIMIT 0, 30 ")or
die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "Name: ".$row['Name'];
echo "Email: ".$row['Email'];
mysql_close($con);
?>
First you need permission to create database in mysql. If you are user, then you can create mysql database.So first you have to check whether the database is connected or not. The php code for create database is:
<?php
$con=mysql_connect('localhost','root','');
if($con)
{
$query=mysql_query('create database new') or die(mysql_error());
$qu=mysql_query('use new');
if($query)
{
echo'Database is created';
}
else
{
echo'Database is not created';
}
}
else
{
echo'The database is not connected';
}
?>
- connect_error) { die("Connection failed: " . $conn->connect_error); } // Create database $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
精彩评论