2
打开我们创建PHP文件:
先设置 地址,账号,密码:
$url = "127.0.0.1"//连接数据库的地址
$user = "root"//账号
$password = "root"//密码
//获取连接$con = mysql_connect($url,$user,$password)
if(!$con){
die("连接失败".mysql_error())
}
3
设置具体连接的数据,那我们这儿连接test数据库,我们通过Navicat 打开mysql 数据库
mysql_select_db("test")
$link = mysql_pconnect("localhost","root","")$sql = 'CREATE DATABASE my_db'
if (mysql_query($sql, $link)) {
echo "成功"
} else {
echo "失败" . mysql_error() . "\n"
}
注:不提倡使用函数mysql_create_db()。最好用mysql_query()来提交一条SQLCREATEDATABASE语句来替代。
先mysql_connect()到mysql再做mysql_query()的相关 *** 作。====================================================================
<?php
$server = "localhost"
$username = "root"
$password = ""
$database = "myabc"
$ranks = array(
1=>"newbie",
2=>"new member",
3=>"member",
4=>"high member",
5=>"very high member",
6=>"supreme member",
7=>"ultra member",
8=>"godlike member",
9=>"god member",
10=>"low god",
11=>"medium god",
12=>"high god",
13=>"very high god",
14=>"supreme god",
15=>"ultra god",
16=>"perfect"
)
$couldNotOpenDatabase = "Could not open database<BR>\n please check your settings in config.php"
$couldNotConnectMysql="Could not connect Mysql!"
$conn=mysql_connect($server,$username,$password) or die ($couldNotConnectMysql)
if (mysql_select_db($database,$conn))
{//数据库存在,做相应 *** 作
}
else
{//数据库不存在,创建一个,并做相应 *** 作
$query = "CREATE DATABASE $database"
$result = mysql_query($query)
mysql_select_db($database,$conn)or die ($couldNotOpenDatabase)
}
?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)