我想创建一个数据库,如果它不存在。代码是在这里:创建数据库,如果不存在php mysql
<?php
// Connect to MySQL
$link = mysql_connect('host', 'user', 'pw');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make studentdb the current database
$db_selected = mysql_select_db('StudentDB', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE StudentDB';
if (mysql_query($sql, $link)) {
echo "Database StudentDB created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
我得到这个错误:
错误创建数据库:拒绝访问用户myusernamehere'数据库 'StudentDB'
用户具有DBA权限...我猜这是我的用户的权限错误....我如何通过脚本授予用户权限并使此脚本有效?
您无法通过这个代码,你需要'grant'您用户权限用您的root用户(或具有授予访问权限的其他用户)创建数据库。 – Jon
[请勿使用'mysql_ *'函数](http://stackoverflow.com/q/12859942/1190388)码。他们不再被维护,并[正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到红色框?改为了解准备好的语句,然后使用[tag:PDO]或[tag:MySQLi]。 – hjpotter92