Home > php教程 > PHP源码 > ZF框架Db类的一些遍历结果集和对表操作的最基本用法

ZF框架Db类的一些遍历结果集和对表操作的最基本用法

PHP中文网
Release: 2016-05-25 16:59:42
Original
1193 people have browsed it

1. [代码]fetchAll()方法(常用)    

//fetchAll()方法,也是最常用的遍历方法
//类似mysql_fetch_array(),如果遍历需要双层foreach循环
$Result = $Db->fetchAll($Select);
Copy after login

2. [代码]fetchObject()使用方法

//fetchObject()使用方法
//以对象形式返回结果集
while ($Result = $State -> fetchObject()) 
{
	echo "ID为:" . $Result->s_id . "<br>";
	echo "姓名为:" . $Result->s_name . "<br>";
	echo "<hr>";
}
Copy after login

3. [代码]fetchOnce()使用方法

//fetchOnce()使用方法
//只读取结果集的一行,一般用作单条查询
$Result = $Db -> fetchOne($Sql);
echo $Result;
Copy after login

4. [代码]fetchAssoc()使用方法

//fetchAssoc()使用方法
//返回联合数组,类似mysql_fetch_assoc()
echo "<table border=&#39;1&#39;>";
foreach ($Result as $key => $value) 
{
	echo "<tr>";
	foreach ($value as $key2 => $value2) 
	{
		echo "<td>". $value2 . "</td>";
	}
	echo "</tr>";
}
echo "</table>";
Copy after login

5. [代码]修改方法

//Zend_Db实例化后直接可以使用的修改方法
$Where = &#39;s_id=1043&#39;;
$Set = array
(
	&#39;s_dengji&#39; => 999
);
$Db -> update($table,$Set,$Where);
Copy after login

6. [代码]删除方法

//Zend_Db实例化后直接可以使用的删除方法
$Mes = "s_id = 1041";
$Db -> delete($table,$Mes);
Copy after login

7. [代码]添加方法

//Zend_Db实例化后直接可以使用的添加方法
$table = &#39;sanguo&#39;;
$Mes = array
(
	&#39;s_id&#39; => &#39;1259&#39;,
	&#39;s_name&#39; => &#39;张三&#39;,
	&#39;s_sheng&#39; => &#39;山东&#39;,
	&#39;s_xian&#39; => &#39;济南&#39;,
	&#39;s_gongzi&#39; => 6000,
	&#39;s_dengji&#39; => 1
);
$Db -> insert($table,$Mes);
Copy after login

8. [代码]查询方法

//Zend_Db实例化后直接可以使用的查询方法
$Sql = "select * from sanguo";
$Result = $Db -> query($Sql);
$Arr = $Result -> fetchAll();

echo "<table width=&#39;500&#39;>";
foreach ($Arr as $row) 
{
	echo "<tr>";
	foreach ($row as $k=>$v)
	{		
		echo "<td>" . $v . "</td>";
	}
	echo "</tr>";
}
echo "</table>";
?>
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template