网站首页 > 文章中心 > 其它

php如何编写数据库

作者:小编 更新时间:2023-10-22 12:22:14 浏览量:96人看过

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

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

①.、连接数据库

php

if?(!$con)

{

php如何编写数据库-图1

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 用类写数据库功能

你的

echo "scriptalert('{$sql}');/script"; 因为是在字符串中引用变量,你那种写法可能不会有内容. 还有 $sql变量通过上面的两个操作,Fn_select函数是没有返回值的,所有$sql=$db-Fn_select获得的$sql值是空的,是没有内容的.你可以在Fn_select函数中设置一个返回值,比如return true,或者return false, $sql就有值了.

php写入数据库

PHP向MySQL数据库中写入数据有三个步骤:

①.,PHP和MySQL建立连接关系

代码如下

mysql_connect("localhost","root","");//连接MySQL

mysql_select_db("hello");//选择数据库

当然,前提是已经安装WEB服务器、PHP和MySQL,并且建立MySQL表"cnbruce"

mysql_connect()中三个参数分别为MySQL地址、MySQL用户名和MySQL密码

然后就是通过WEB页面传递数据,让PHP通过SQL语句将数据写入MySQL数据库指定的表中,比如新建文件 post.php

require_once("conn.php");//引用数据库链接文件

$uname = $_GET['n'];//GET方法为URL参数传递

$psw = $_GET['p'];

$sql = "insert into members(username,password) values ('$uname','$psw')";

mysql_query($sql);//借SQL语句插入数据

mysql_close();//关闭MySQL连接

echo "成功录入数据";

补充:读取表

读取表中的内容,这里我们用while,可以根据具体情况,用for 或其他的.

while($row = mysql_fetch_array($result))

echo $row['Topic'] . "br/";

如何应用PHP开发Web数据库

进入php源程序目录中的ext目录中,这里存放着各个扩展模块的源代码,选择你需要的模块,比如curl模块:cd curl执行phpize生成编译文件!

可能会报错:Cannot find autoconf. Please check your autoconf installation andthe $PHP_AUTOCONFenvironment variable is set correctly and then rerun thisscript.,需要安装autoconf:yum install autoconf(RedHat或者CentOS)、apt-get installautoconf(Ubuntu Linux)!

如果有配置错误,这里会报错,可以根据错误信息去排查!

以上就是土嘎嘎小编为大家整理的php如何编写数据库相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章