当前位置:知识百科 > 正文

php创建数数据表的简单实现_php数据表创建命令代码

更新时间:2025-01-18 04:00 阅读量:22761

如何用ThinkPHP新建数据库表

THINKPHP貌似没有自己的方法创建数据表.

能想到的有:在thinkphp里执行原生的创建表语句.

怎么实现php自动创建数据库,像discuz安装时那样,自己建数据库和表?

你做好程序以后,把数据库导出成sql文件(这个文件里就已经有了一下创建数据表,添加数据记录等的一些sql语句了)

新建一个安装文件:

①.、连接数据库(安装的时候不是要填写一些数据库连接参数等吗)

就这么简单,思路是这样啊,具体这么实现,你自己慢慢研究

如何实现PHP自动创建数据库

你做好程序以后,把数据库导出成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;

??}?else?{

???$this-sqlArr[]?=?$v;

//执行sql文件

public?function?RunSql()?{

?foreach?($this-sqlArr?as?$k?=?$v)?{

??if?(!mysql_query($v))?{

???exit("sql语句错误:第"?.?$k?.?"行"?.?mysql_error());

?return?true;

//范例:

$sql?=?new?ReadSql("localhost",?"root",?"",?"log_db");

$rst?=?$sql-Import("./log_db.sql");

if?($rst)?{

echo?"Success!";

如何在php创建数据库与数据表

创建数据库:create database 数据库名

创建数据表:

CREATE TABLE +users+ (

+id+ tinyint(10) auto_increment primary key NOT NULL,

+age+ int(10) NOT NULL

执行这两个sql语句就行

以上就是韩罡百科网小编为大家整理的php创建数数据表的简单实现相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!