php 循环结果集 新手
本帖最后由 zuoan2008 于 2013-07-06 15:47:24 编辑
class_db.php
<br />
<?php<br />
Class MySQLDB<br />
{<br />
var $host;<br />
var $user;<br />
var $passwd;<br />
var $database;<br />
var $conn;<br />
<br />
//利用构造函数实现变量初始化<br />
//同时连接数据库操作<br />
function MySQLDB($host,$user,$password,$database)<br />
{<br />
$this->host = $host;<br />
$this->user = $user;<br />
$this->passwd = $password;<br />
$this->database = $database;<br />
$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or die("Could not connect to $this->host");<br />
mysql_select_db($this->database,$this->conn) or die("Could not switch to database $this->database");<br />
mysql_query("set names 'gbk'");<br />
}<br />
<br />
//该函数用来关闭数据库连接<br />
function Close()<br />
{<br />
MySQL_close($this->conn);<br />
}<br />
<br />
//该函数实现数据库查询操作<br />
function Query($queryStr)<br />
{<br />
$res =Mysql_query($queryStr, $this->conn);<br />
return $res;<br />
}<br />
<br />
//该函数返回记录集<br />
function getRows($res)<br />
{<br />
$rowno = 0;<br />
$rowno = MySQL_num_rows($res);<br />
if($rowno>0)<br />
{<br />
for($row=0;$row<$rowno;$row++ )<br />
{<br />
<br />
$rows[$row]=MySQL_fetch_array($res);<br />
<br />
//本来为MySQL_fetch_row,但是不能以数组的方式来提取,只能用索引<br />
<br />
//这样可以用索引和名称,更为方便<br />
<br />
}<br />
<br />
return $rows;<br />
<br />
}<br />
<br />
}<br />
<br />
//该函数取回数据库记录数<br />
function getRowsNum($res)<br />
{<br />
$rowno = 0;<br />
$rowno = mysql_num_rows($res);<br />
return $rowno;<br />
}<br />
<br />
//该函数返回数据库表字段数<br />
function getFieldsNum($res)<br />
{<br />
$fieldno = 0;<br />
$fieldno = mysql_num_fields($res);<br />
return $fieldno;<br />
<br />
}<br />
<br />
//该函数返回数据库表字段名称集<br />
function getFields($res)<br />
{<br />
$fno = $this->getFieldsNum($res);<br />
if($fno>0)<br />
{<br />
for($i=0;$i<$fno;$i++ )<br />
{<br />
$fs[$i]=MySQL_field_name($res,$i);//取第i个字段的名称<br />
}<br />
return $fs;<br />
<br />
}<br />
<br />
}<br />
<br />
} <br />
Nach dem Login kopieren
login_action.php
<br>
<?php <br />
session_start();<br>
include('../config/config_db.php');<br>
include('../class/db/class_db.php'); <div class="clear">
</div>
Nach dem Login kopieren