资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php引用数据库 php如何调用数据库

PHP网站怎么连接到数据库?

常规方式

创新互联公司专注于广州企业网站建设,成都响应式网站建设公司,商城系统网站开发。广州网站建设公司,为广州等地区提供建站服务。全流程按需定制设计,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

常规方式就是按部就班的读取文件了。其余的话和上述方案一致。

// 读取配置文件内容

$handle = fopen("filepath", "r");            $content = fread($handle, filesize("filepath"));123

PHP解析XML

上述两种读取文件,其实都是为了PHP解析XML来做准备的。关于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是对于比较小型的xml配置文件,simplexml就足够了。

配置文件

?xml version="1.0" encoding="UTF-8" ?mysql

!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 --

hostlocalhost/host

userroot/user

password123456/password

dbtest/db

port3306/port/mysql12345678910

解析

?php/**

* 作为解析XML配置文件必备工具

*/class XMLUtil {

public static $dbconfigpath = "./db.config.xml";    public static function getDBConfiguration() {

$dbconfig = array ();        try {            // 读取配置文件内容

$handle = fopen(self::$dbconfigpath, "r");            $content = fread($handle, filesize(self::$dbconfigpath));            // 获取xml文档根节点,进而获取相关的数据库信息

$mysql = simplexml_load_string($content);            // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用

$dbconfig['host'] = $mysql-host;            $dbconfig['user'] = $mysql-user;            $dbconfig['password'] = $mysql-password;            $dbconfig['db'] = $mysql-db;            $dbconfig['port'] = $mysql-port;            // 将配置信息以关联数组的形式返回

return $dbconfig;

} catch ( Exception $e ) {            throw new RuntimeException ( "mark读取数据库配置文件信息出错!/markbr /" );

}        return $dbconfig;

}

}1234567891011121314151617181920212223242526272829

数据库连接池

对于PHP程序而言,优化永无止境。而数据库连接池就在一定程度上起到了优化的作用。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升。

于是,这里简单的模拟了一下数据库连接池的实现。核心在于维护一个“池”。

从池子中取,用毕,归还给池子。

?php/**x

*  PHP中的数据库 工具类设计

*  郭璞

*  2016年12月23日

*

**/class DbHelper {    private $dbconfig;    private $dbpool;    public $poolsize;    public function __construct($poolsize = 20) {        if (! file_exists ( "./utils.php" )) {            throw new RuntimeException ( "markutils.php文件丢失,无法进行配置文件的初始化操作!/markbr /" );

}else {

require './utils.php';

}        // 初始化 配置文件信息

$this-dbconfig = XMLUtil::getDBConfiguration ();        // 准备好数据库连接池“伪队列”

$this-poolsize = $poolsize;

$this-dbpool = array ();        for($index = 1; $index = $this-poolsize; $index ++) {

$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark连接数据库失败!/markbr /" );

array_push ( $this-dbpool, $conn );

}

}    /**

* 从数据库连接池中获取一个数据库链接资源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() {        if (count ( $this-dbpool ) = 0) {            throw new ErrorException ( "mark数据库连接池中已无链接资源,请稍后重试!/mark" );

} else {            return array_pop ( $this-dbpool );

}

}    /**

* 将用完的数据库链接资源放回到数据库连接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) {        if (count ( $this-dbpool ) = $this-poolsize) {            throw new ErrorException ( "mark数据库连接池已满/markbr /" );

} else {

array_push ( $this-dbpool, $conn );

}

}

}

php怎么把数据导入数据库

需要PHP基础知识和数据库基础知识。

以SQL为例。使用PHP MySQL 函数可以编辑数据库。

mysql_connect() 函数打开MySQL 连接。举例

?php

$con = mysql_connect("localhost","mysql_user","mysql_pwd");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}// 一些代码...mysql_close($con);

?

mysql_connect()三个参数分别是服务器名,连接账号,连接密码。

连接之后,可以使用mysql_select_db()设置要处理的数据库,后面则是用数据库语句处理数据。SQL语法简介网页链接

PHP调用三种数据库的方法(3)

Oracle(甲骨文)是世界上最为流行的关系数据库。它是大公司推崇的工业化的强有力的引擎。我们先看看其相关的函数:

(1)integer

ora_logon(string

user

,

string

password)

开始对一个Oracle数据库服务器的连接。

(2)integer

ora_open(integer

connection)

打开给出的连接的游标。

(3)integer

ora_do(integer

connection,

string

query)

在给出的连接上执行查询。PHP生成一个指示器,解析查询,并执行之。

(4)integer

ora_parse(integer

cursor,

string

query)

解析一个查询并准备好执行。

(5)boolean

ora_exec(integer

cursor)

执行一个先前由ora_parse函数解析过的查询。

(6)boolean

ora_fetch(integer

cursor)

此函数会使得一个执行过的查询中的行被取到指示器中。这使得您可以调用ora_getcolumn函数。

(7)string

ora_getcolumn(integer

cursor,

integer

column)

返回当前的值。列由零开始的数字索引。

(8)boolean

ora_logoff(integer

connection)

断开对数据库服务器的链接。

以下是向ORACLE数据库插入数据的示例程序:

html

headtitle向ORACLE数据库中插入数据/title/head

body

form

action="?echo

$PHP_SELF;?"

method="post"

table

border="1"

cellspacing="0"

cellpadding="0"

tr

thID/th

thname/th

thDescription/th

/tr

tr

tdinput

type="text"

name="name"

maxlength="50"

size="10"/td

tdinput

type="text"

name="email"

maxlength="255"

size="30"/td

tdinput

type="text"

name="Description"

maxlength="255"

size="50"/td

/tr

tr

align="center"

td

colspan="3"input

type="submit"

value="提交" input

type="reset"

value="重写"/td

/tr

/table

/form

?

//先设置两个环境变量ORACLE_HOME,ORACLE_SID

putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");

putenv("ORACLE_SID=ora8");

//设置网页显示中文

putenv("NLS_LANG=Simplified_Chinese.zhs16cgb231280");

if($connection=ora_logon("scott","tiger"))

{

//库表test有ID,name,Description三项

$sql

=

'insert

into

test(ID,name,Description)

values

';

$sql

.=

'(''

.

$ID

.

'',''

.

$name

.

'',''.

$Description

.

'')';

if($cursor=ora_do($connect,$sql))

{

print("insert

finished!");

}

$query

=

'select

*

from

test';

if($cursor=ora_do($connect,$query))

{

ora_fetch($cursor);

$content0=ora_getcolumn($cursor,0);

$content1=ora_getcolumn($cursor,1);

$content2=ora_getcolumn($cursor,2);

print("$content0");

print("$content1");

print("$content2");

ora_close($cursor);

}

ora_logoff($connection);

}

?

/body

/html

php调用数据库字段

我说一下几个步骤:

1、首先你得有一个存储这些数据的数据库表,比如数据库表的结构是这样的。

数据库表名为:user

字段:编号(id),姓名(name),手机(mobile),产品名称(productName) 主键为id

2、实现你需要的功能:

第一步:你需要连接数据库,有一个连接数据库的文件:conn.php。内容如下:

// 我假设你的数据库是mysql的,假设你的数据库用户名为root,密码为123456,根据你数据库的实际情况改写成你的。数据库名称假设为db_889888658

?php

$conn=mysql_connect("localhost","root","123456") or die("数据库连接失败,请检查用户名或密码");

mysql_select_db("db_889888658",$conn);

mysql_query("SET NAMES 'gb2312'");

?

第二步:你需要一个添加数据的表单,就相当于一个注册或添加数据的页面。如文件为:add.html内容如下:

form action="reg.php" method="post"

input type="text" name="name"br/

input type="text" name="mobile"br/

input type="text" name="productName"/br

input type="submit" name="submit" value="添加数据"

/form

第三步:写一个处理你表单提交的数据的文件reg.php。内容如下:

?php

include "conn.php";

if(isset($_POST["submit"])){

$name=$_POST["name"];

$mobile=$_POST["mobile"];

$productName=$_POST["productName"];

$sql="INSERT INTO 'user'(id,name,mobile,productName) VALUES (NULL,$name,$mobile,$productName)";

$query=mysql_query($sql);

$num=mysql_affected_rows($conn);

if($num=1){

echo "scriptalert('数据添加成功');location.href='add.html';/script";

}else{

echo "scriptalert('数据添加失败');history.back();/script";

}

}

?

第四步,第三步已经实现你说的第一个功能。下面说一下你的第二个功能。写一个表单,输入你要查询的手机号,点击“查询”按钮查询你想要的字段。

?php

if($_POST["submit"]){

$mobile=$_POST["mobile"];

if(!empty($mobile)){

include "conn.php";

$sql="SELECT * FROM 'user' WHERE 'mobile'='$mobile'";

$query=mysql_query($sql);

while($rs=mysql_fetch_array($query)){

$str="查询结果:br/";

$str.="用户名:".$rs["name"]." ";

$str.="产品名:".$rs["name"]." ";

}

echo "您查询的手机号为".$mobile."的数据信息如下:br/";

echo $str;

}else{

echo "请输入手机号";

}

}

?

form action="" method="post"

请输入您要查询的手机号:input type="text" name="mobile" input type="submit" name="submit" value="查询"

/form

php 调用数据库怎么调用

?php

mysql_connect("localhost","root","123456") //填写mysql用户名和密码

or die("Could not connect to MySQL server!");

mysql_select_db("phpcms") //数据库名

or die("Could not select database!");

mysql_query('set names "gbk"'); //数据库内数据的编码

?


当前题目:php引用数据库 php如何调用数据库
网站URL:http://cdkjz.cn/article/doohgpe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220