php空间怎么进入数据库 php怎么访问数据库

如何用php连接mdb数据库?以下为几个php连接access数据库和操作acess数据的方法,全部做成了类,应用起来也更方便,也可摘用其中的部分代码应用 。
?php
--------------------------------------------------------------------
//FileName:class.php
//Summary: Access数据库操作类
//使用范例:
//$databasepath="database.mdb";
//$dbusername="";
//$dbpassword="";
//include_once("class.php");
//$access=new Access($databasepath,$dbusername,$dbpassword);
--------------------------------------------------------------------
class Access
{
var $databasepath,$constr,$dbusername,$dbpassword,$link;
function Access($databasepath,$dbusername,$dbpassword)
{
$this-databasepath=$databasepath;
$this-username=$dbusername;
$this-password=$dbpassword;
$this-connect();
}
function connect()
{
$this-constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this-databasepath);
$this-link=odbc_connect($this-constr,$this-username,$this-password,SQL_CUR_USE_ODBC);
return $this-link;
//if($this-link) echo "恭喜你,数据库连接成功!";
//else echo "数据库连接失败!";
}
function query($sql)
{
return @odbc_exec($this-link,$sql);
}
function first_array($sql)
{
return odbc_fetch_array($this-query($sql));
}
function fetch_row($query)
{
return odbc_fetch_row($query);
}
function total_num($sql)//取得记录总数
{
return odbc_num_rows($this-query($sql));
}
function close()//关闭数据库连接函数
{
odbc_close($this-link);
}
function insert($table,$field)//插入记录函数
{
$temp=explode(',',$field);
$ins='';
for ($i=0;$icount($temp);$i)
{
$ins.="'".$_POST[$temp[$i]]."',";
}
$ins=substr($ins,0,-1);
$sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
$this-query($sql);
}
function getinfo($table,$field,$id,$colnum)//取得当条记录详细信息
{
$sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";
$query=$this-query($sql);
if($this-fetch_row($query))
{
for ($i=1;$i$colnum;$i)
{
$info[$i]=odbc_result($query,$i);
}
}
return $info;
}
function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得记录列表
{
$sql="SELECT * FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
$recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);
$i;
}
return $recordlist;
}
function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得记录列表
{
$sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
for ($j=0;$j$fieldnum;$j)
{
$info[$j]=odbc_result($query,$j 1);
}
$rdlist[$i]=$info;
$i;
}
return $rdlist;
}
function updateinfo($table,$field,$id,$set)//更新记录
{
$sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleteinfo($table,$field,$id)//删除记录
{
$sql="DELETE FROM ".$table." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleterecord($table,$condition)//删除指定条件的记录
{
$sql="DELETE FROM ".$table." WHERE ".$condition;
$this-query($sql);
}
function getcondrecord($table,$condition="")// 取得指定条件的记录数
{
$sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
$query=$this-query($sql);
$this-fetch_row($query);
$num=odbc_result($query,1);
return $num;
}
}
?
22222222
class.php文件:
[php]
?php
class Access//Access数据库操作类
{
var $databasepath,$constr,$dbusername,$dbpassword,$link;//类的属性
function Access($databasepath,$dbusername,$dbpassword)//构造函数
{
$this-databasepath=$databasepath;
$this-username=$dbusername;
$this-password=$dbpassword;
$this-connect();
}
function connect()//数据库连接函数
{
$this-constr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this-databasepath);
$this-link=odbc_connect($this-constr,$this-username,$this-password,SQL_CUR_USE_ODBC);
return $this-link;
//if($this-link) echo "恭喜你,数据库连接成功!";
//else echo "数据库连接失败!";
}
function query($sql)//送一个查询字符串到数据库中
{
return @odbc_exec($this-link,$sql);
}
function first_array($sql)//从access数据库中返回一个数组
{
return @odbc_fetch_array($this-query($sql));
}
function fetch_row($query)//返回记录中的一行
{
return odbc_fetch_row($query);
}
function total_num($sql)//取得记录总数
{
return odbc_num_rows($this-query($sql));
}
function close()//关闭数据库连接函数
{
odbc_close($this-link);
}
function insert($table,$field)//插入记录函数
{
$temp=explode(',',$field);
$ins='';
for ($i=0;$i {
$ins.="'".$_POST[$temp[$i]]."',";
}
$ins=substr($ins,0,-1);
$sql="INSERT INTO ".$table." (".$field.") VALUES (".$ins.")";
$this-query($sql);
}
function getinfo($table,$field,$id,$colnum)//取得当条记录详细信息
{
$sql="SELECT * FROM ".$table." WHERE ".$field."=".$id."";
$query=$this-query($sql);
if($this-fetch_row($query))
{
for ($i=1;$i$colnum;$i)
{
$info[$i]=odbc_result($query,$i);
}
}
return $info;
}
function getlist($table,$field,$colnum,$condition,$sort="ORDER BY id DESC")//取得记录列表
{
$sql="SELECT * FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
$recordlist[$i]=getinfo($table,$field,odbc_result($query,1),$colnum);
$i;
}
return $recordlist;
}
function getfieldlist($table,$field,$fieldnum,$condition="",$sort="")//取得记录列表
{
$sql="SELECT ".$field." FROM ".$table." ".$condition." ".$sort;
$query=$this-query($sql);
$i=0;
while ($this-fetch_row($query))
{
for ($j=0;$j$fieldnum;$j)
{
$info[$j]=odbc_result($query,$j 1);
}
$rdlist[$i]=$info;
$i;
}
return $rdlist;
}
function updateinfo($table,$field,$id,$set)//更新记录函数
{
$sql="UPDATE ".$table." SET ".$set." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleteinfo($table,$field,$id)//删除记录函数
{
$sql="DELETE FROM ".$table." WHERE ".$field."=".$id;
$this-query($sql);
}
function deleterecord($table,$condition)//删除指定条件的记录函数
{
$sql="DELETE FROM ".$table." WHERE ".$condition;
$this-query($sql);
}
function getcondrecord($table,$condition="")//取得指定条件的记录数函数
{
$sql="SELECT COUNT(*) AS num FROM ".$table." ".$condition;
$query=$this-query($sql);
$this-fetch_row($query);
$num=odbc_result($query,1);
return $num;
}
}
?
[/php]
数据库连接文件:
[php]
?php
$databasepath="data/database.mdb";//数据库路径
$dbusername="";//数据库用户名
$dbpassword="";//数据库密码
include_once("class.php");//调用数据库操作类
$access=new Access($databasepath,$dbusername,$dbpassword);//新建一个数据库操作类的对象
?
[/php]
[php]
?php
$sql="select * from $info where id=$id";
$result=$access-query($sql)or die("error2");
$array=odbc_fetch_array($result);
?
[/php]
333333333333
这个是为了 同时可以使用access和mysql而做的 先弄一个mysql的 然后又写一个access的 所有的函数一一对应 你可以看下 绝对原创喔~~
配置文件如下
$config['db']['type'] = "Mysql"; //数据库类型“Mysql” , “Access”
$config['db']['database']= "ourcms"; //数据库(文件)名
$config['db']['host'] = ""; //数据库主机
$config['db']['username']= "7king"; //数据库连接用户名
$config['db']['password']= "tingting"; //数据库连接密码
/*
$config['db']['type'] = "Access"; //数据库类型“Mysql”,“Access”
$config['db']['database']= "ourcms.mdb";//数据库(文件)名
$config['db']['host'] = "";
$config['db']['username']= "";
$config['db']['password']= "";
?php
/**
* 2007.04 by zhaohe
*
* php连接access通用类
*
* 用法:
* 建立new Access类 = set_db设置数据路径 = set_login 设置连接数据库的用户名和密码
* = 通过set_conn 设置连接 =
* {
get_result 获取查询执行结果; get_result_rows 获取查询执行列表,一般是select
insert_info 插入新的记录 update_info更新记录
}
*
*
*/
class Access {
/**
* 类变量定义
* @param $conn mysql连接号
* @param $error 错误代号
* @param $username/$password 数据库连接用户名和密码
* @param array $err_info 错误信息
*
* @param $debuginfo 调试信息
* @param $table 当前操作数据表
*/
var $conn;
var $error;
var $database;
var $username = "";
var $password = "";
var $err_info = array(
0 = "没有错误!",
1 = "数据库连接失败!",
2 = "sql执行出错!"
);
var $debuginfo="";
var $table;
/**
* 默认构造方法
**/
function Access( $arr=null ){
if( is_array($arr) ) {
$this-set_login( $arr['host'] , $arr['username'] , $arr['password'] );
$this-set_db( $arr['database'] );
$this-set_conn();
}
}
/**
* 设置数据库文件名
* @param string $dbfile
*
* return void
*/
function set_db ( $dbfile ){
$this-database = $dbfile;
}
/**
* 设置连接数据库的用户名和密码
* @param string $user 用户名
* @param string $pwd 密码
*
* @return void
*/
function set_login ( $user , $pwd ){
$this-username=$user;
$this-password=$pwd;
}
/**
* 创建数据库连接
* @param
* return void
*/
function set_conn ( ){
if($this-conn=odbc_connect("DRIVER=Microsoft Access Driver (*.mdb);DBQ=".realpath($this-database),$this-username,$this-password,SQL_CUR_USE_ODBC )) $this-error=0;
else $this-error=1;
}
/**
* 设置当前操作的数据表
* @param string $tb
*
* @return void
*/
function set_table( $tb ) {
$this-table = $tb;
}
/**
* 返回sql查询结果
* @param string $sql sql语句
*
* @return #id
*/
function get_result( $sql ){
return odbc_do( $this-conn , $sql );
}
/**
* 获取查询的结果
* @param string $sql
*
* @return array 结果的二维数组
*/
function get_result_rows( $sql ){
$array = array() ;
$result = $this-get_result( $sql );
while( $row = odbc_fetch_array( $result ) )
$array[] = $row ;
return $array;
}
/**
* 获取部分查询结果
*
* @param Array 数组
* @return Array
*/
function get_query_result( $cols , $tb=null , $order=null , $limit=null , $start=0 ) {
if( empty($tb) ) $tb=$this-table;
else $this-table=$tb;
if( is_array($cols) ) $col="[".implode('],[',$cols)."]";
else $col = $cols;
if( empty($limit) )
$sql = "select $col from $tb";
else
$sql ="select top $limit $col from $tb";;
if( isset($order) ) $sql.=" order by $order";
return $this-get_result_rows($sql);
}
/**
* 执行数据库插入操作
*
* @param $arr values列表,数组索引为数据表字段
* @param $tb 操作数据表 如果为空则为设置的当前类的操作表
*/
function insert_info( $arr , $tb = "" ) {
$cols = array_keys( $arr );
$values = array_values( $arr );
if (empty($tb)) $tb = $this-tb;
/*
foreach( $arr as $key = $value ){
$cols[] = $key;
$values[] = $value;
}
*/
$sql = "insert into [$tb]([".implode("],[",$cols)."]) values('".implode("','",$values)."')";
//return $sql;
return $this-get_result( $sql );
}
/**
* 执行数据库更新操作
*
* @param array $arr 要更新的字段值 数组索引为表字段名
* @param array $con 条件数组
* @param string $tb 要操作的数据表
*
*/
function update_info( $arr , $con , $tb = "" ) {
$cols = array();
$conditions = array();
if (empty( $tb )) $tb = $this-tb;
foreach( $arr as $key = $value ){
$cols[] = "[$key]='$value'";
}
foreach( $con as $key = $value ) {
//检查数据类型
if( is_int($value) || is_float($value) )
$conditions[] = "[$key]=$value";
else
$conditions[] = "[$key]='$value'";
}
$sql = "update [$tb] set ".implode(",",$cols)." where ".implode(" and ",$conditions);
//return $sql;
return $this-get_result( $sql );
}
}
?
mysql的类如下
class Mysql {
/**
* mysql连接执行类 , 将sql的执行实现数据库无关性
*
*
*
*/
/**
* 类变量定义
* @param $conn mysql连接号
* @param $error 错误代号
* @param $username/$password 数据库连接用户名和密码
* @param array $err_info 错误信息
*
* @param $debuginfo 调试信息
* @param $table 当前操作数据表
*/
var $conn;
var $error;
var $username = "";
var $password = "";
var $host;
var $database;
var $err_info = array(
0 = "没有错误!",
1 = "数据库连接失败!",
2 = "sql执行出错!"
);
var $debuginfo="";
var $table;
function Mysql( $arr=null ) {
if( is_array($arr) ) {//var_dump($arr);
$this-set_login( $arr['host'] , $arr['username'] , $arr['password'] );
$this-set_db( $arr['database'] );
$this-set_conn();
if( isset($this-error)$this-error!=0 ) die($this-err_info[$this-error]);
}
}
/**
* 设置数据库名
* @param string $database
*
* return void
*/
function set_db ( $dbfile ){
$this-database = $dbfile;
}
/**
* 设置连接数据库的用户名和密码
* @param string $user 用户名
* @param string $pwd 密码
*
* @return void
*/
function set_login ( $host , $user , $pwd ){
$this-host=$host;
$this-username=$user;
$this-password=$pwd;
}
/**
* 创建数据库连接
* @param
* return void
*/
function set_conn (){
$this-conn=mysql_connect($this-host,$this-username,$this-password );
if ( isset($this-conn)mysql_select_db($this-database) )
$this-error=0;
else
$this-error=1;
}
/**
* 设置当前操作的数据表
* @param string $tb
*
* @return void
*/
function set_table( $tb ) {
$this-table = $tb;
}
/**
* 返回sql查询结果
* @param string $sql sql语句
*
* @return #id
*/
function get_result( $sql ){
return mysql_query( $sql , $this-conn );
}
/**
* 获取查询的结果
* @param string $sql
*
* @return array 结果的二维数组
*/
function get_result_rows( $sql ){
$array = array() ;
$result = $this-get_result( $sql );
while( $row = mysql_fetch_assoc( $result ) )
$array[] = $row ;
return $array;
}
/**
* 获取部分查询结果
*
* @param Array 数组
* @return Array
*/
function get_query_result( $cols , $tb=null , $condition , $order=null , $limit=null , $start=0 ) {
if( empty($tb) ) $tb=$this-table;
else $this-table=$tb;
if( is_array($cols) ) $col="`".implode('`,`',$cols)."`";
else $col = $cols;
if( isset($limit) )
$sql.="select top $limit $col from $tb";
else
$sql = "select $col from $tb";
if( isset($condition) ) $sql.=" where $condition";
if( isset($order) ) $sql.=" order by $order";
if( isset($limit) ) $sql.=" limit $start,$limit";
return $this-get_result_rows($sql);
}
/**
* 执行数据库插入操作
*
* @param $arr values列表,数组索引为数据表字段
* @param $tb 操作数据表 如果为空则为设置的当前类的操作表
*/
function insert_info( $arr , $tb = "" ) {
$cols = array_keys( $arr );
$values = array_values( $arr );
if (empty($tb)) $tb = $this-table;
/*
foreach( $arr as $key = $value ){
$cols[] = $key;
$values[] = $value;
}
*/
$sql = "insert into [$tb](`".implode("`,`",$cols)."`) values('".implode("','",$values)."')";
//return $sql;
return $this-get_result( $sql );
}
/**
* 执行数据库更新操作
*
* @param array $arr 要更新的字段值 数组索引为表字段名
* @param array $con 条件数组
* @param string $tb 要操作的数据表
*
*/
function update_info( $arr , $con , $tb = "" ) {
$cols = array();
$conditions = array();
if (empty( $tb )) $tb = $this-table;
if( is_array($arr) ) {
foreach( $arr as $key = $value ){
$cols[] = "`$key`='$value'";
}
foreach( $con as $key = $value ) {
//检查数据类型
if( is_int($value) || is_float($value) )
$conditions[] = "`$key`=$value";
else
$conditions[] = "`$key`='$value'";
}
$sql = "update `$tb` set ".implode(",",$cols)." where ".implode(" and ",$conditions);
}
else
$sql = "update `$tb` set $arr where $con";
//return $sql;
return $this-get_result( $sql );
}
}
如何在PHP空间phpmyadmin下导入 SQL.txt 安装数据库登录phpmyadmin后,点击左边窗口的数据库名称,然后点击右边窗口的Import,上传SQL.txt即可 。
通常php网页常常会连接到数据库,那么这个数据库在哪里?要怎么配置phpphp空间怎么进入数据库的搭档php空间怎么进入数据库,很多都是mysqlphp空间怎么进入数据库,sql数据库服务这个你要安装 , 没有安装你哪来的数据库啊 , zend说白了,其实只是一个编辑器而已,并没有数据库的成分 。
windows系统,可以使用wamp,等集成环境来安装 。wamp本身自带phpmysqladmin,可以查看数据库,另外,使用外部工具navicat蛮好用的,也可以用来查看数据库
在服务器上做了一个PHP程序的网站 , 现在又在息壤了个PHP的空间 如何把数据库自服务器上传到空间里?服务器是你的,那你可以直接打包mysql源文件
然后到新的空间后台,上传压缩包
然后提交客服帮你导入新空间数据库中
服务,这是你花钱买的
如果 。。。遇到没服务的商家 , 最好另外花钱买服务了 。。。
【php空间怎么进入数据库 php怎么访问数据库】php空间怎么进入数据库的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php怎么访问数据库、php空间怎么进入数据库的信息别忘了在本站进行查找喔 。

    推荐阅读