phpMyadmin是一个被普遍应用的网络数据库管理系统,使用起来较为简单,可以自动创建,也可以运行SQL语句创建,下面分别演示两种方式创建Mysql数据库的步骤:
第一段:使用菜单自动创建数据库
登陆phpMyAdmin
在php MyAdmin右边窗口中,填写数据库名称,点创建即可. 例如我们这里创建一个名字为:cncmstest 的数据库
创建成功之后提示如下
第二段:运行SQL语句创建数据库
在SQL输入窗口输入建表语句,点右下角执行即可,创建db_xuesheng数据库为例代码如下
create?database?db_xuesheng;
执行成功界面如下
备注:其实两种创建数据库方式本质是一样的,第一种是利用系统菜单自动组合建库SQL并执行,第二种是手动输入建库SQL,如果熟悉SQL操作的话,第二种更快捷.
phpstudy数据库创建步骤:
你做好程序以后,把数据库导出成sql文件
①.、连接数据库
php
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
if?(mysql_query("CREATE?DATABASE?my_db",$con))
echo?"Database?created";
else
echo?"Error?creating?database:?"?.?mysql_error();
mysql_close($con);
class?ReadSql?{
//数据库连接
protected?$connect?=?null;
//数据库对象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql语句集
public?$sqlArr?=?array();
public?function?__construct($host,?$user,?$pw,?$db_name)?{
$host?=?empty($host)?C("DB_HOST")?:?$host;
$user?=?empty($user)?C("DB_USER")?:?$user;
$pw?=?empty($pw)?C("DB_PWD")?:?$pw;
$db_name?=?empty($db_name)?C("DB_NAME")?:?$db_name;
//连接数据库
$this-connect?=?mysql_connect($host,?$user,?$pw)?or?die("Could?not?connect:?"?.?mysql_error());
$this-db?=?mysql_select_db($db_name,?$this-connect)?or?die("Yon?can?not?select?the?table:"?.?mysql_error());
//导入sql文件
public?function?Import($url)?{
$this-sqlFile?=?file_get_contents($url);
if?(!$this-sqlFile)?{
exit("打开文件错误");
}?else?{
$this-GetSqlArr();
if?($this-Runsql())?{
return?true;
//获取sql语句数组
public?function?GetSqlArr()?{
//去除注释
$str?=?$this-sqlFile;
$str?=?preg_replace('/--.*/i',?'',?$str);
$str?=?preg_replace('/\/\*.*\*\/(\;)?/i',?'',?$str);
//去除空格?创建数组
$str?=?explode(";\n",?$str);
foreach?($str?as?$v)?{
$v?=?trim($v);
if?(empty($v))?{
continue;
$this-sqlArr[]?=?$v;
//执行sql文件
public?function?RunSql()?{
foreach?($this-sqlArr?as?$k?=?$v)?{
if?(!mysql_query($v))?{
exit("sql语句错误:第"?.?$k?.?"行"?.?mysql_error());
//范例:
$sql?=?new?ReadSql("localhost",?"root",?"",?"log_db");
$rst?=?$sql-Import("./log_db.sql");
if?($rst)?{
echo?"Success!";
以上就是土嘎嘎小编为大家整理的php创建一个数据库相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!