23、php与OOP
---class.php---
class database
{
public $num,$result;
public function query($sql)
{
$this->result=@mysql_query($sql) or die("err code 3: sql query fail") ;
return $this->result; //返回查询结果集
@mysql_free_result($result) or die("err code 5: mysql_free_result() fail");
}
public function num_rows($sql)
{
$this->result=$this->query($sql); //通过$this->query引用query函数. $this->result引用成员变量result!!
$this->num=@mysql_num_rows($result) or die("err code 4: mysql_num_rows() fail");
return $this->num; //此函数返回查询的结果数。
}
}
-------test.php--------
<?php
include_once("class.php");
$conn=new database;
$sql="select * from user order by userAge";
echo $conn->num_rows($sql)."<br>";
?>
//-------------------php与Ajax------------------------------------
由于jsvascrīpt采用UTF8编码,在windows下采用ajax回送服务器的返回信息就会出现乱码,
因此在windows下应用时,php处理页面ajax请求的页面必须包含:
header('Content-Type:text/html;charset=GB2312');这句话,否则php获取的中文数据返回到
ajax的html界面将显示乱码。
//输出xml
<?
require_once("conn.php");
header("Conten-Type:text/xml");
echo "<?xml version="1.0" encoding="gb2312"?>";
echo "<menuitems>";
$result = mysql_db_query("myly","select * from myTable");
while($row = mysql_fetch_object($result)) {
echo "<item><species>".$row->name."</species>";
echo "<location><![CDATA[".$row->location."]]></location></item>";
}
echo "</menuitems>";
mysql_free_result($result);
mysql_close();
?>
//输出xml---
<?php
header("Conten-Type:text/xml");
echo "<?xml version="1.0" encoding="gb2312"?>";
echo "<user><row>";
for($i=0;$i<5;$i++)
{
$str="<col{$i}>{$i}</col{$i}>";
echo $str;
}
echo "</row></user>";
?>