1.  > 网站建设

phpmysql网站开发项目式教程(php mysql项目)

phpmysql网站开发项目式教程(php mysql项目)

本篇文章给大家谈谈phpmysql网站开发项目式教程,以及php mysql项目对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享phpmysql网站开发项目式教程的知识,其中也会对php mysql项目进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

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

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

你做好程序以后,把数据库导出成sql文件

1、连接数据库

2、读取这个sql文件里的sql语句,并执行

3、生成一个数据库连接参数的php文件

lt;?php

$con = mysql_connect("localhost","peter","abc123");

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);

?gt;

lt;?php

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-gt;connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());

$this-gt;db = mysql_select_db($db_name, $this-gt;connect) or die("Yon can not select the table:" . mysql_error());

}

//导入sql文件

public function Import($url) {

$this-gt;sqlFile = file_get_contents($url);

if (!$this-gt;sqlFile) {

exit("打开文件错误");

} else {

$this-gt;GetSqlArr();

if ($this-gt;Runsql()) {

return true;

}

}

}

//获取sql语句数组

public function GetSqlArr() {

//去除注释

$str = $this-gt;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-gt;sqlArr[] = $v;

}

}

}

//执行sql文件

public function RunSql() {

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

if (!mysql_query($v)) {

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

}

}

return true;

}

}

//范例:

header("Content-type:text/html;charset=utf-8");

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

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

if ($rst) {

echo "Success!";

}

?gt;

关于phpmysql网站开发项目式教程和php mysql项目的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 phpmysql网站开发项目式教程的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php mysql项目、phpmysql网站开发项目式教程的信息别忘了在本站进行查找喔。

[免责声明]本文来源于网络,不代表本站立场,如转载内容涉及版权等问题,请联系邮箱:3801085100#qq.com,#换成@即可,我们会予以删除相关文章,保证您的权利。 转载请注明出处:http://www.0755gszc.com/hdss/18018.html