您的位置:首页 > 编程语言 > PHP开发

php连接access方法

2014-05-16 17:53 435 查看
<?PHP

/**
* @package
*/
class php2access
{
//文件路径
public $file_path;
//access db路径
public $db_path;
//conn
private $conn;
function __construct()
{
$this->file_path = "new/67/";
$this->db_path = "#database#/#database#.mdb";
$this->access_init();

}

function access_init()
{
return true;
$conn=new com("adodb.connection");
$connstr="driver={microsoft access driver (*.mdb)};dbq=".realpath($this->db_path);
$conn->open($connstr);
$this->conn = $conn;
}

function action_index()
{
$files_ary = $this->getAllFiles($this->file_path);
$files_ary = $this->_filter_files_name($files_ary);
//$this->re_filename($files_ary);//重命名
$this->insert_response($files_ary);//insert

}

function re_filename($files_ary)
{
if(!is_array($files_ary))exit('不是数组!');

$arr = array();
foreach ($files_ary as $key => $value)
{
$old_filename = $this->file_path.$value['infoimg_old'];
$new_filename = $this->file_path.$value['infoimg'];
if(!file_exists($old_filename)) continue;
//rename(iconv('UTF-8','GBK',$old_filename), iconv('UTF-8','GBK',$new_filename));
rename($old_filename, $new_filename);
}

}

//所有文件 return array
function getAllFiles($file_path = '')
{
if(!$file_path)
{
echo('路径不存在!');
return true;
}

$files_ary = '';

//先判断数组文件是否存在。
$ary_filename = 'aryfile_'.str_replace("/", "_", $file_path).'.txt';
if(file_exists($ary_filename))
{
$files_str = file_get_contents($ary_filename);
$files_ary = unserialize($files_str);
return $files_ary;
}

//
$handler = opendir($file_path);

while (($filename = readdir($handler)) !== false)
{
if (!is_dir($file_path.'/'.$filename))
{
$files_ary[] = $filename;
}
}
//写入文件
$files_str = serialize($files_ary);
$fp = fopen($ary_filename, "w");
fwrite($fp, $files_str);
fclose($fp);
return $files_ary;
}

/**
* @package
* 0=>array(filename, md5(filename).jpg),
* @return [type]
*/
function _filter_files_name($files_ary)
{
if(!is_array($files_ary))exit('不是数组!');

$arr = array();
foreach ($files_ary as $key => $value)
{
$ary = explode(".", $value);
$ext = array_pop($ary);
$infotitle = implode(".",$ary);
$arr[$key]['infotitle'] = $infotitle;
$arr[$key]['infoimg_old'] = $infotitle.'.'.$ext;
$arr[$key]['infoimg'] = md5($infotitle).'.'.$ext;
}
return $arr;
}

function insert_response($files_ary)
{
if(!is_array($files_ary))exit('不是数组!');
$info_class_id = explode("/", $this->file_path);
$info_class_id = $info_class_id[1];

foreach ($files_ary as $key => $value)
{
$infotitle = $value['infotitle'];
$content = $value['infotitle'];
$infocontent = $value['infotitle'];
$infoimg = $info_class_id.'/'.$value['infoimg'];
$str = "
sql=\"insert into info (InfoTitle,content,Info_Class_id,InfoContent,infoimg) values ('{$infotitle}','{$content}',{$info_class_id},'{$infocontent}','{$infoimg}')\" \n
Set rs=server.CreateObject(\"adodb.recordset\") \n
Conn.execute(sql) \n
";
echo $str;
}

}

function insert($files_ary)
{
return true;

if(!is_array($files_ary))exit('不是数组!');

$rs=new com("adodb.recordset");

foreach ($files_ary as $key => $value)
{
$infotitle = $value['infotitle'];
$content = $value['infotitle'];
$infocontent = $value['infotitle'];
$infoimg = $value['infoimg'];
$rs->open("INSERT INTO info (infotitle,content,infocontent,infoimg) VALUSE ('{$infotitle}','{$content}','{$infocontent}','{$infoimg}') ",$conn,1,1);
}
$rs->close();
}

function select()
{
$conn = $this->conn;
$rs = new com("adodb.recordset");
$rs->open("select * from infotitle WHERE info_class_id=66 ",$conn,1,1);
while(!$rs->eof){
//echo $rs->Fields["newstitle"]->Value;
print_r($rs->Fields);
echo "<br/>";
$rs->Movenext(); //
}
$rs->close();
}

}

$ary = array(67,68,69,73,74,76,100);
$a = new php2access();

foreach ($ary as $key => $value)
{
$a->file_path = "new/{$value}/";
$a->action_index();
}

//$a->select();

exit;

///////////////////////////////&**&///////////////////////////////////////
$db = new PDO("odbc:driver={microsoft access driver (*.mdb)};dbq=".realpath("#database#/#database#.mdb")) or die("Connect Error");

$sql_insert = "INSERT INTO info (infotitle,info_class_id) VALUES ('".rand()."',66)";
$sql = "select * from news";
$rs = $db->query($sql_insert);
print "<pre>";
//print_r($rs->fetchAll());
print "</pre>";
$dsn = "DRIVER=Microsoft Access Driver (*.mdb);dbq=".realpath("#database#/#database#.mdb");
$conn = @odbc_connect($dsn,"","",SQL_CUR_USE_ODBC ) or die ("Connect Error!");

$sql = "select * from info where info_class_id=66";
$rs = @odbc_do($conn,$sql);

while(odbc_fetch_row($rs)){
echo odbc_result($rs,"infoid")."---".odbc_result($rs,"infotitle");
echo "<br/>";
}

//$sql_insert = "INSERT INTO info (infotitle,info_class_id) VALUES ('".rand()."',66)":
// $rs = odbc_do($conn,$sql_insert);
odbc_close($conn);

<?PHP

$conn=new com("adodb.connection");
$connstr="driver={microsoft access driver (*.mdb)};dbq=".realpath("#database#/#database#.mdb");
$conn->open($connstr);

$rs=new com("adodb.recordset");
$rs->open("select * from news ",$conn,1,1);

$conn->execute("INSERT INTO news (newstitle,newscontent) VALUES ('AA----+++', 'AABBB--++++');");

/*
while(!$rs->eof){
echo $rs->Fields["newstitle"]->Value;
echo "<br/>";
$rs->Movenext();     //将记录集指针下移
}
*/

//所有文件 return array
function getAllFiles()
{
$
}

$rs->close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: