PHP代码创建Mysql数据库

文件名“config.php”,在其他文件中都被引用。“config.php”执行后出现“Couldnotopendatabasepleasecheckyoursetti... 文件名“config.php”,在其他文件中都被引用。“config.php”执行后出现“Could not open database
please check your settings in config.php”。下面是可能涉及到得代码:

$server = "localhost";
$username = "root";
$password = "123";
$database = "database_cs";
$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";
f ($database == ""){
// Check it the database exist
$query = "use database_cs";
// If this query is succesfull, and returns a 1. this means that the database members can be used.
// If it returns 'null' then it doesn't exist.
if (mysql_query($query) == null){
//if it doesn't exist we create it and connect to it.
$query = "CREATE DATABASE database_cs";
$result = mysql_query($query);
if ($result == 1){
$conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);
}else{echo "Error while creating database (Errornumber ". mysql_errno() .": \"". mysql_error() ."\")<br>";}
}
else{
// It already exist so we will connect to it.
$database= "database_cs";
$conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);
//echo "数据库选择成功了!";
}
}
else{
// connect or show an error.
$conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);
}

没有分了,哪位高手帮忙分析下,谢了。现在在做毕业设计“网上选课系统”,在网上下载的代码。比较急,各位救救急。
“$database=database_cs;”这条代码应该屏蔽掉;Mysql密码是“123”;
然后测试出现下面的错误提示:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\AppServ\www\ISC\config.php on line 92

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\AppServ\www\ISC\config.php on line 92

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\AppServ\www\ISC\config.php on line 95

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\AppServ\www\ISC\config.php on line 95
Error while creating database (Errornumber 1045: "Access denied for user 'ODBC'@'localhost' (using password: NO)")
展开
 我来答
Apon_zone
2010-05-26 · 超过21用户采纳过TA的回答
知道答主
回答量:36
采纳率:0%
帮助的人:54.6万
展开全部
先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);

}
?>
piplive
2010-05-25 · TA获得超过1194个赞
知道小有建树答主
回答量:188
采纳率:100%
帮助的人:146万
展开全部
问题出在两行 $conn = mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
mysql_select_db($database,$conn) or die ($couldNotOpenDatabase);,问题可能出现在你的,$username,$password,$database变量里边
.最有可能是你的mysql的密码不为123造成的,要么把MySQL里的密码改为123,要么把$password改为你mysql里的密码。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式