我想創建一個數據庫,如果它不存在。代碼是在這裏:創建數據庫,如果不存在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