php中提供了直接与mssql数据库连接的命令,就像是与mysql连接一样,下面我来介绍。
下面是odbc建立连接代码。
代码如下 |
复制代码 |
$con = odbc_connect('odbc名称','用户名','密码');
|
2、连接mssql2000
代码如下 |
复制代码 |
$con = mssql_connect('数据库地址','用户名','密码');
|
3、连接mssql2008
代码如下 |
复制代码 |
$connectionInfo = array("UID"=>用户名,"PWD"=>密码,"Database"=>"数据库名称");
$con = sqlsrv_connect( 数据库地址,$connectionInfo);
|
上面方法很简单我就不介绍了,只讲述如果连接不成功我们可做如下设置(php.ini文件
中;extension=php_pdo_mssql.dll ;extension=php_pdo_odbc.dll 前面的分号去掉)
我们经常会使用phpExcel导入或导入xls文件,但是如果一次导出数据比较大就会出现内存溢出错误,下面我来总结解决办法。
phpExcel将读取的单元格信息保存在内存中,我们可以通过
代码如下 |
复制代码 |
PHPExcel_Settings::setCacheStorageMethod()
|
来设置不同的缓存方式,已达到降低内存消耗的目的!
1、将单元格数据序列化后保存在内存中
代码如下 |
复制代码 |
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
|
2、将单元格序列化后再进行Gzip压缩,然后保存在内存中
代码如下 |
复制代码 |
PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
|
3、缓存在临时的磁盘文件中,速度可能会慢一些
代码如下 |
复制代码 |
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
|
4、保存在php://temp
代码如下 |
复制代码 |
PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
|
5、保存在memcache中
PHPExcel_CachedObjectStorageFactory::cache_to_memcache
举例:
第4中方式:
代码如下 |
复制代码 |
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '8MB'
);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
|
第5种:
代码如下 |
复制代码 |
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;
$cacheSettings = array( 'memcacheServer' => 'localhost',
'memcachePort' => 11211,
'cacheTime' => 600
);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
|
其它的方法
第一个方法,你可以考虑生成多个sheet的方式,不需要生成多个excel文件,根据你数据总量计算每个sheet导出多少行, 下面是PHPExcel生成多个sheet方法:
面是PHPExcel生成多个sheet方法:
代码如下 |
复制代码 |
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1',$x);
$sheet->setCellValue('B1',$y);
|
第二个方法,你可以考虑ajax来分批导出,不用每次刷新页面。
代码如下 |
复制代码 |
<a href="#" id="export">export to Excel</a>
$('#export').click(function() {
$.ajax({
url: "export.php",
data: getData(), //这个地方你也可以在php里获取,一般读数据库
success: function(response){
window.location.href = response.url;
}
})
});
|
代码如下 |
复制代码 |
<?php
//export.php
$data = $_POST['data'];
$xls = new PHPExcel();
$xls->loadData($formattedData);
$xls->exportToFile('excel.xls');
$response = array(
'success' => true,
'url' => $url
);
header('Content-type: application/json');
echo json_encode($response);
?>
|
数据量很大的话,建议采用第二种方法,ajax来导出数据,上面方法简单给了个流程,具体你自己补充!
使用php程序的朋友都知道php与mysql非常不错的一对了,我们常常用它来做WEB开发,下面我来介绍最简单的php连接mysql数据库并写入数据,有需要的朋友可参考。
原理步骤
1,php利用mysql_connect与mysql数据库连接
2,打开MySQL数据库
3,接受页面数据,PHP录入到指定的表中
4、然后利用mysql_query把sql执行并保存到数据库中。
连接mysql数据库超级简单如下
代码如下 |
复制代码 |
<?php
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
?>
|
例1
代码如下 |
复制代码 |
<?php
require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
?>
|
结合了更简单的
代码如下 |
复制代码 |
<?
$uname = $_GET['n'];//GET方法为URL参数传递
$psw=md5($_GET['p']);//直接使用MD5加密
$conn=mysql_connect("localhost","root","");//连接MySQL
$sql = "insert into members set username='uname',password='$psw'";
mysql_db_query("hello",$sql,$conn);//借SQL语句插入数据
?>
|
下面来分享一个带缓存数据功能的mysqli类,有需要的同学可看看。
代码如下 |
复制代码 |
<?php
/**
* Mysqli类
*
* @author 废墟
* @version v1.0 2009-08-18
*/
class db_mysqli {
protected $mysqli;
protected $sql;
protected $rs;
protected $query_num = 0;
protected $fetch_mode = MYSQLI_ASSOC;
protected $cache_dir = './cache/';
protected $cache_time = 1800;
public function __construct($dbhost, $dbuser, $dbpass, $dbname) {
$this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
$this->mysqli = false;
echo '<h2>'.mysqli_connect_error().'</h2>';
die();
} else {
$this->mysqli->set_charset("utf8");
}
}
public function __destruct() {
$this->free();
$this->close();
}
protected function free() {
@$this->rs->free();
}
protected function close() {
$this->mysqli->close();
}
protected function fetch() {
return $this->rs->fetch_array($this->fetch_mode);
}
protected function getQuerySql($sql, $limit = null) {
if (@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is", $limit) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-9]+)?$/is", $sql)) {
$sql .= " LIMIT " . $limit;
}
return $sql;
}
protected function get_cache($sql,$method) {
include_once './cache.php';//若框架中使用__autoload(),这里可以不用加载文件
$cache = new cache($this->cache_dir,$this->cache_time);
$cache_file = md5($sql.$method);
$res = $cache->get_cache($cache_file);
if(!$res) {
$res = $this->$method($sql);
$cache->set_cache($cache_file, $res);
}
return $res;
}
public function query_num() {
return $this->query_num;
}
public function set_cache_dir($cache_dir) {
$this->cache_dir = $cache_dir;
}
public function set_cache_time($cache_time) {
$this->cache_time = $cache_time;
}
public function query($sql, $limit = null) {
$sql = $this->getQuerySql($sql, $limit);
$this->sql = $sql;
$this->rs = $this->mysqli->query($sql);
if (!$this->rs) {
echo "<h2>".$this->mysqli->error."</h2>";
die();
} else {
$this->query_num++;
return $this->rs;
}
}
public function getOne($sql) {
$this->query($sql, 1);
$this->fetch_mode = MYSQLI_NUM;
$row = $this->fetch();
$this->free();
return $row[0];
}
public function get_one($sql) {
return $this->getOne($sql);
}
public function cache_one($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getOne');
}
public function getRow($sql, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, 1);
$this->fetch_mode = $fetch_mode;
$row = $this->fetch();
$this->free();
return $row;
}
public function get_row($sql, $fetch_mode = MYSQLI_ASSOC) {
return $this->getRow($sql);
}
public function cache_row($sql) {
$sql = $this->getQuerySql($sql, 1);
return $this->get_cache($sql, 'getRow');
}
public function getAll($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
$this->query($sql, $limit);
$all_rows = array();
$this->fetch_mode = $fetch_mode;
while($rows = $this->fetch()) {
$all_rows[] = $rows;
}
$this->free();
return $all_rows;
}
public function get_all($sql, $limit = null, $fetch_mode = MYSQLI_ASSOC) {
return $this->getAll($sql);
}
public function cache_all($sql, $limit = null) {
$sql = $this->getQuerySql($sql, $limit);
return $this->get_cache($sql, 'getAll');
}
public function insert_id() {
return $this->mysqli->insert_id();
}
public function escape($str) {
if(is_array($str)) {
foreach($str as $key=>$val) {
$str[$key] = $this->escape($val);
}
} else {
$str = addslashes(trim($str));
}
return $str;
}
}
//用法
$db = new db_mysqli('localhost', 'root', 111222, 'dict');
$db->set_cache_time(10);
$db->set_cache_dir('./cache/sql/');
$sql = "select * from words order by word_id limit 10,10";
$res1 = $db->get_all($sql);
$res2 = $db->cache_all($sql);
echo $db->query_num(),'<br>';
?>
|
数据库操作类是所有程序都可以实现的一个功能,下面我来分享一个PHP mysql数据库操作类,这个数据库操作类的特点是对所有保存到数据库的内容进入了过滤,可以很好的处理sql注入了。
代码如下 |
复制代码 |
<?php
//数据库处理类
class db
{
//SQL执行后的数据保存变量;
var $db;
//读取或设置当前数据的位置
var $position=0;
//执行SQL语句并把结果保存为db变量中;
function sub_sql($str)
{
global $prefix;//全局函数,表前缀
return str_replace("#@__",$prefix,$str);
}
function Sql($str)
{
$str=$this->sub_sql($str);
$result = mysql_query($str);
$i=0;
while($row = mysql_fetch_array($result))
{
$str_array[$i]=$row;
$i++;
}
if(empty($str_array))
{
$str_array=array();
}
$this->db=$str_array;
}
//读取一条数据并把数据往后移一位,如果数据为空则返回为null;
function Get_One()
{
$re=empty($this->db[$this->position])?null:$this->db[$this->position];
$this->position=$re?$this->position+1:$this->position;
return $re;
}
//判断是否数据读取到结尾了
function Judge()
{
$re=empty($this->db[$this->position])?true:false;
return $re;
}
//取得db里面的个数
function Get_Num()
{
return count($this->db);
}
//更新数据库里面的数据,$t为表名,$v格式为数组格式,上标为字段名,下标为数据;$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
function Set_Updata($t,$v,$w,$p=0)
{
$this->Sql($t);
$v_str="";
$w_str="";
$f="";
foreach($v as $key=>$vaule)
{
if(!is_numeric($key))
{
if(empty($v_str))
{
$v_str=htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
}else
{
$v_str=$v_str.",".htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
}
}
}
switch($p)
{
case 0:
$f="=";
break;
case 1:
$f=">";
break;
case -1:
$f="<";
break;
}
if(!empty($f))
{
foreach($w as $key=>$vaule)
{
if(!is_numeric($key))
{
if(empty($v_str))
{
$w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}else
{
$w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}
}
}
}
$sql="UPDATE ".$t." SET ".$v_str." where ".$w_str;
return $result = mysql_query($sql);
}
//删除一数据$w为条件上标为字段名下标为数据,$p为条件0为等号,1为大于,-1为小于;
function Set_Del($t,$w,$p=0)
{
$this->sub_sql($t);
$w_str="";
$f="";
switch($p)
{
case 0:
$f="=";
break;
case 1:
$f=">";
break;
case -1:
$f="<";
break;
}
if(!empty($f))
{
foreach($w as $key=>$vaule)
{
if(!is_numeric($key))
{
if(empty($v_str))
{
$w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}else
{
$w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
}
}
}
}
$str="DELETE FROM ".$t." WHERE ".$w_str;
return $result = mysql_query($str);
}
function Add($t,$v)
{
$this->sub_sql($t);
$k_str="";
$v_str="";
foreach($v as $key=>$vaule)
{
if(!is_numeric($key)){
if(empty($k_str))
{
$k_str=htmlspecialchars($key);
$v_str="'".htmlspecialchars($vaule)."'";
}else
{
$k_str=$k_str.",".htmlspecialchars($key);
$v_str=$v_str.","."'".htmlspecialchars($vaule)."'";
}
}
}
$str="INSERT INTO ".$t."(".$k_str.")"."value(".$v_str.")";
return $result = mysql_query($str);
}
}
?>
|