Home  >  Article  >  Backend Development  >  php常用技术

php常用技术

WBOY
WBOYOriginal
2016-06-23 14:32:02945browse

1.       连接数据库(connect.inc.php):

$dbhost = 'localhost';//数据库服务器

$dbuser ='root';//数据库用户名

$dbpwd = '';//数据库密码

$dbname = '';//数据库名        

$db=new mysqli($dbhost,$dbuser,$dbpwd,$dbname);//建立一个mysql连接对象

if(mysqli_connect_errno())//检查是否连接成功

{

           echo 'Error:Could not connect to database'.$dbname.'Please try again later';

           exit;

}

$db->query("set names 'utf8';"); //设定字符集

?>

在建立数据库连接后,通过引用include(“connect.inc.php”)就可以使用$db对象

2.       DOMdocument建立xml文件

Xml是网络数据通信的重要形式,另一种则为json。

创建DOMdocument 对象: 

$dom=new DOMdocument('1.0','iso-8859-1');

$dom->formatOutput = true; 

创建根结点:

        $root=$dom->appendChild($dom->createElement('root'));

为根结点添加元素:

$root>appendChild($log=$dom->createElement("log",$row['log_content']));

为节点添加属性:

$log->appendChild(new DOMAttr("id",$row['log_id']));

$log->appendChild(new DOMAttr("title",$row['log_name']));

$log->appendChild(new DOMAttr("date",$row['log_date']));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php 简明语法Next article:首次使用php