©
This document uses PHP Chinese website manual Release
(PHP 4, PHP 5)
mysql_create_db — 新建一个 MySQL 数据库
$database name
[, resource $link_identifier
] )mysql_create_db() 尝试在指定的连接标识所关联的服务器上建立一个新数据库。
database_name
要创建的数据库名。
link_identifier
MySQL
连接。如不指定连接标识,则使用由 mysql_connect()
最近打开的连接。如果没有找到该连接,会尝试不带参数调用
mysql_connect()
来创建。如没有找到连接或无法建立连接,则会生成
E_WARNING
级别的错误。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 替代 mysql_create_db() 的例子
mysql_create_db() 函数已废弃。最好用 mysql_query() 来提交一条 SQL 的 CREATE DATABASE 语句来代替。
<?php
$link = mysql_connect ( 'localhost' , 'mysql_user' , 'mysql_password' );
if (! $link ) {
die( 'Could not connect: ' . mysql_error ());
}
$sql = 'CREATE DATABASE my_db' ;
if ( mysql_query ( $sql , $link )) {
echo "Database my_db created successfully\n" ;
} else {
echo 'Error creating database: ' . mysql_error () . "\n" ;
}
?>
以上例程的输出类似于:
Database my_db created successfully
Note:
本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除。应使用 MySQLi 或 PDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南以及相关 FAQ 以获取更多信息。用以替代本函数的有:
- mysqli_query()
- PDO::query()
Note:
为了向下兼容,可以使用下列已废弃的别名: mysql_createdb()
Note:
如果 MySQL 扩展是基于 MySQL 4.x 客户端库编译的话则本函数不可用。
[#1] zubrag [2006-08-29 03:03:56]
mysql_create_db function will not work on cPanel hosting. If you need to create database from your PHP script on cPanel hosted server then you'll need to use cPanel interface. Database creation code would look like (calling cPanel's adddb function): http://USER:PASS@HOST:2082/frontend/SKIN/sql/adddb.html?db=DB
You can download ready-made sample php script from http://www.zubrag.com/scripts/cpanel-database-creator.php
[#2] omer [2005-06-20 17:42:51]
for MySQL4.1 lib users;
as noted the function is a no-go for MySQL4.1 libs. While i do not know the logic behind this i was relieved to see that
mysql_connect ("localhost","$user","$password")
$soru = 'CREATE DATABASE '.$dbname;
mysql_query($soru);
worked just fine...