php中实现数据库 php实现数据库增删改查demo

PHP如何实现一个高效的数据库

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

玛纳斯网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联于2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

1、连接数据库

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

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

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

?

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

}

}

//范例:

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

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

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

if ($rst) {

echo "Success!";

}

?

PHP基于单例模式实现的数据库操作基类

本文实例讲述了PHP基于单例模式实现的数据库操作基类。分享给大家供大家参考,具体如下:

配置文件:

?php

$db

=

array(

'host'='localhost',

'user'='root',

'password'='',

'database'='test',

)

?

php

数据库基类:

?php

class

db

{

public

$conn;

public

static

$sql;

public

static

$instance=null;

private

function

__construct(){

require_once('db.config.php');

$this-conn

=

mysql_connect($db['host'],$db['user'],$db['password']);

if(!mysql_select_db($db['database'],$this-conn)){

echo

"失败";

};

mysql_query('set

names

utf8',$this-conn);

}

public

static

function

getInstance(){

if(is_null(self::$instance)){

self::$instance

=

new

db;

}

return

self::$instance;

}

/**

*

查询数据库

*/

public

function

select($table,$condition=array(),$field

=

array()){

$where='';

if(!empty($condition)){

foreach($condition

as

$k=$v){

$where.=$k."='".$v."'

and

";

}

$where='where

'.$where

.'1=1';

}

$fieldstr

=

'';

if(!empty($field)){

foreach($field

as

$k=$v){

$fieldstr.=

$v.',';

}

$fieldstr

=

rtrim($fieldstr,',');

}else{

$fieldstr

=

'*';

}

self::$sql

=

"select

{$fieldstr}

from

{$table}

{$where}";

$result=mysql_query(self::$sql,$this-conn);

$resuleRow

=

array();

$i

=

0;

while($row=mysql_fetch_assoc($result)){

foreach($row

as

$k=$v){

$resuleRow[$i][$k]

=

$v;

}

$i++;

}

return

$resuleRow;

}

/**

*

添加一条记录

*/

public

function

insert($table,$data){

$values

=

'';

$datas

=

'';

foreach($data

as

$k=$v){

$values.=$k.',';

$datas.="'$v'".',';

}

$values

=

rtrim($values,',');

$datas

=

rtrim($datas,',');

self::$sql

=

"INSERT

INTO

{$table}

({$values})

VALUES

({$datas})";

if(mysql_query(self::$sql)){

return

mysql_insert_id();

}else{

return

false;

};

}

/**

*

修改一条记录

*/

public

function

update($table,$data,$condition=array()){

$where='';

if(!empty($condition)){

foreach($condition

as

$k=$v){

$where.=$k."='".$v."'

and

";

}

$where='where

'.$where

.'1=1';

}

$updatastr

=

'';

if(!empty($data)){

foreach($data

as

$k=$v){

$updatastr.=

$k."='".$v."',";

}

$updatastr

=

'set

'.rtrim($updatastr,',');

}

self::$sql

=

"update

{$table}

{$updatastr}

{$where}";

return

mysql_query(self::$sql);

}

/**

*

删除记录

*/

public

function

delete($table,$condition){

$where='';

if(!empty($condition)){

foreach($condition

as

$k=$v){

$where.=$k."='".$v."'

and

";

}

$where='where

'.$where

.'1=1';

}

self::$sql

=

"delete

from

{$table}

{$where}";

return

mysql_query(self::$sql);

}

public

static

function

getLastSql(){

echo

self::$sql;

}

}

$db

=

db::getInstance();

//$list

=

$db-select('demo',array('name'='tom','password'='ds'),array('name','password'));

//echo

$db-insert('demo',array('name'='脚本之家','password'='123'));

//echo

$db-update('demo',array("name"='xxx',"password"='123'),array('id'=1));

echo

$db-delete('demo',array('id'='2'));

db::getLastSql();

echo

"pre";

?

更多关于PHP操作数据库相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《PHP基于pdo操作数据库技巧总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

PHP表单提交不同数据到数据库的不同表的不同字段怎么实现?

你可以用 PHP 的 MySQLi 或 PDO 库来实现。

首先,你需要使用这些库中的函数连接数据库,然后把表单中的字段值插入到相应的数据表中。

例如,如果你使用 MySQLi,你可以这样写:

$conn = mysqli_connect("hostname", "username", "password", "database_name");

$rwid = $_POST['rwid'];

$rwdj = $_POST['rwdj'];

$rwzb = $_POST['rwzb'];

$sql1 = "INSERT INTO table_a (s) VALUES ('$rwid')";

mysqli_query($conn, $sql1);

$sql2 = "INSERT INTO table_b (d) VALUES ('$rwdj')";

mysqli_query($conn, $sql2);

$sql3 = "INSERT INTO table_c (f) VALUES ('$rwzb')";

mysqli_query($conn, $sql3);

如果你使用 PDO,你可以这样写:

$conn = new PDO("mysql:host=hostname;dbname=database_name", "username", "password");

$rwid = $_POST['rwid'];

$rwdj = $_POST['rwdj'];

$rwzb = $_POST['rwzb'];

$stmt1 = $conn-prepare("INSERT INTO table_a (s) VALUES (:s)");

$stmt1-bindParam(':s', $rwid);

$stmt1-execute();

$stmt2 = $conn-prepare("INSERT INTO table_b (d) VALUES (:d)");

$stmt2-bindParam(':d', $rwdj);

$stmt2-execute();

$stmt3 = $conn-prepare("INSERT INTO table_c (f) VALUES (:f)");

$stmt3-bindParam(':f', $rwzb);

$stmt3-execute();

希望这个答案对你有帮助。


网页标题:php中实现数据库 php实现数据库增删改查demo
文章源于:http://hbruida.cn/article/dosejgh.html