php连接数据教程 PHP连接

php连接access数据库代码

php教程

创新互联公司是一家专业提供温州企业网站建设,专注与成都网站制作、成都网站设计、外贸营销网站建设H5建站、小程序制作等业务。10年已为温州众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

连接access数据库教程代码

下面提供三种php连接access数据库方法,一种是利用php的pdo,一种是odbc,com接口来与access数据库连接哦。

*/

//利用pdo与access数据库连接

$path

="f:font";

$conn

=

new

pdo("sqlite:$path");

if(

$conn

)

{

echo

('connection

pdo

success');

}

else

{

echo

('cnnection

pdo

fail

,plase

check

database

server!');

}

//利用

odbc_connect连接数据库

$conn

=

odbc_connect("dbdsn","admin","123");

//连接数据源

$doquery=odbc_exec($conn,"select

*

from

表名

where

条件");//执行查询

//利用com接口连接access数据库

$conn=new

com("adodb.connection");

$dsn="driver={microsoft

access

driver

(*.mdb)};dbq=".realpath("path/db1.mdb");

$conn-open($dsn);

php如何连接数据库

$mysql_host="localhost";  地址

$mysql_user="root";       用户名

$mysql_password="123";    密码

$mysql_database="001online";   数据库名

$conn=mysql_connect("$mysql_host","$mysql_user","$mysql_password");

if(!$conn){ die("连接数据库失败:" . mysql_error());}

mysql_select_db("$mysql_database",$conn);

mysql_query("set character set 'utf-8'");

mysql_query("set names 'utf-8'");

使用php百度BAE怎么连接数据库啊

以下是BAE的连接MySQL方法。基本的调用和PHP的相关MySQL函数一样:

mysql_query — 发送一条 MySQL 查询

mysql_fetch_array — 从结果集中取得一行作为关联数组,或数字数组,或二者兼有

mysql_fetch_row — 从结果集中取得一行作为枚举数组

mysql_fetch_assoc — 从结果集中取得一行作为关联数组

mysql_result — 取得结果数据

教程手册:点击查阅

[php] view plaincopy

?php

/*连接到汪海实验室的BAE数据库*/

/*从平台获取查询要连接的数据库名称*/

$dbname ='cIvsXiIejIxQjRUtnrme';

/*从环境变量里取出数据库连接需要的参数*/

$host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');

$port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');

$user = getenv('HTTP_BAE_ENV_AK');

$pwd = getenv('HTTP_BAE_ENV_SK');

/*接着调用mysql_connect()连接服务器*/

$link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);

if(!$link) {

die("Connect Server Failed: " . mysql_error($link));

}

/*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/

if(!mysql_select_db($dbname,$link)) {

die("Select Database Failed: " . mysql_error($link));

}

/*至此连接已完全建立,可以使用其它标准php mysql函数操作进行数据库操作*/

$sql = "SELECT * FROM `Users` LIMIT 0, 30;";

//$sql = "INSERT INTO `Users` (`Name`, `Email`, `Age`, `Sex`) VALUES ('Test01', 'test@sina.cn', '12', '1');";

$result = mysql_query("$sql")

or die("Invalid query: " . mysql_error());

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

print_r($row);

}

echo "That's all!";

?

以下是SAE的连接方式,很多都已经封装好了,直接使用SQL语句即可:

[php] view plaincopy

?php

$mysql = new SaeMysql();

//查询

$sql = "SELECT * FROM `user` LIMIT 10";

$data = $mysql-getData( $sql );

$name = strip_tags( $_REQUEST['name'] );

$age = intval( $_REQUEST['age'] );

//插入

$sql = "INSERT INTO `user` ( `name` , `age` , `regtime` ) VALUES ( '" . $mysql-escape( $name ) . "' , '" . intval( $age ) . "' , NOW() ) ";

$mysql-runSql( $sql );

if( $mysql-errno() != 0 )

{

die( "Error:" . $mysql-errmsg() );

}

$mysql-closeDb();

?

连接phpAdmin的时候如果长时间没有操作会提示网页过期,此时CTRL+F5即可。

按F5有时候一些内容是不会被更新的,而CTRL+F5则所有内容都会被更新.

具体区别是:

F5通常只是刷新本地缓存;

Ctrl+F5可以把Intenet临时文件夹的文件删除再重新从服务器下载,也就是彻底刷新页面。

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中连接MySQL数据库

php链接mysql必备条件:已安装mysql数据库;

检查php环境是否已开启mysql扩展(一般情况下是开启的);

检查方法:a.使用phpinfo();函数,看有没有mysql项;b.打开php.ini文件,检查php_mysql.dll前分号是否已取掉。

php链接代码如下:

运行结果:


本文名称:php连接数据教程 PHP连接
浏览路径:http://myzitong.com/article/doospco.html