PHP中使用mysql处理结果集

一.从结果集中将记录取出
    mysql_fetch_row($result); 从结果集中取得一行作为枚举数组

mysql_fetch_row($result

    mysql_fetch_assoc($result);从结果集中取得一行作为关联数组

mysql_fetch_assoc($result)

        以上两个没有什么效率区分
    mysql_fetch_array($result);从结果集中取得一行作为关联数组,或数字数组,或二者兼有

mysql_fetch_array($result)

    mysql_fetch_object($result);从结果集中取得一行作为对象

mysql_fetch_object($result)

    使用一次就从结果集中取出一条记录
    将指针移动到下一条记录(默认是第一条记录)
    如果取完就返回false    
    mysql_data_seek($result,row)  移动内部结果的指针

mysql_data_seek($result,2)

二.从结果集中获取列的信息
    mysql_field_name()取得结果中指定字段的字段名

<?php
//连接MySQL数据库
$link=mysql_connect("localhost","root","") or die("连接MySQL数据库失败!");
//选择数据库
mysql_select_db($link,"xsphp")or die("选择数据库失败!");
//执行SQL语句
$sql="select id,name,price,num,desn from shops";
$result=mysql_query($link,$sql);
$cols=mysql_num_fields($result); $rows=mysql_num_rows($result); echo '<table align="center" width="800" border="1">';
echo '<caption>演示表</caption>';
echo '<tr>';
for($i=0;$i<$cols;$i++){
    echo '<th>';
    mysql_field_name($result,$i);
    echo '</th>';
}
echo '</tr>';
//取出结果集中记录的信息
while(list($id,$name,$price,$num,$desn)=mysql_fetch_row($result)){echo '<tr>';
    echo '<td>'.$id.'</td>';
    echo '<td>'.$name.'</td>';
    echo '<td>'.$price.'</td>';
    echo '<td>'.$num.'</td>';
    echo '<td>'.$desn.'</td>';
    echo '</tr>';
}
echo '</table>';

/*//移动内部结果的指针
mysql_data_seek($result,2);
//从结果集中将记录取出
while($data=mysql_fetch_assoc($result)){
    print_r($data);
    echo '<br>';
}

//print_r($data);
//var_dump($result);
*/

//释放结果集
mysql_free_result($result); //关闭连接
mysql_close($link); 
?>

分页函数

<?php

    //连接mysql数据库
    $link=mysql_connect("localhost","root","") or die("连接mysql数据库失败!");
    //选择数据库
    mysql_select_db("xsphp")or die("选择数据库失败!");

function table($tabname){
    //执行SQL语句
    $num=10;

    $sql="select * from {$tabname}";
    $result=mysql_query($sql);
    $total=mysql_num_rows($result);  //记录总数

    $url="2.php";   //每次请求的URL

    $cpage=isset($_GET["page"])?$_GET["page"]:1;  //当前页

    $pagenum=ceil($total/$num);  //总页数

    $offset=($cpage-1)*$num;    //开始取数据的位置

    $sql="select * from {$tabname} limit {$offset},{$num}";

    $result=mysql_query($sql);
    
    $start=$offset+1;   //开始记录
    $end=($cpage==$pagenum)?$total:($cpage*$num);  //结束记录

    $next=($cpage==$pagenum)? $pagenum : ($cpage+1);

    $prev=($cpage==1)? 1 : ($cpage-1);

    $cols=mysql_num_fields($result); $rows=mysql_num_rows($result); 
    echo '<table align="center" width="800" border="1">';
    echo '<caption>{$tabname}</caption>';
    echo '<tr>';
    for($i=0;$i<$cols;$i++){
    echo '<th>';
    //mysql_field_name($result,$i);
    echo '</th>';
    }
    echo '</tr>';
    //取出结果集中记录的信息
    while(list($id,$name,$price,$num,$desn)=mysql_fetch_row($result)){
    echo '<tr>';
    echo '<td>'.$id.'</td>';
    echo '<td>'.$name.'</td>';
    echo '<td>'.$price.'</td>';
    echo '<td>'.$num.'</td>';
    echo '<td>'.$desn.'</td>';
    echo '</tr>';
    }
    echo '<tr><td colspan="'.$cols.'" align="right">';
    echo "共<b>{$total}</b>条记录, 本页显示<b>{$start}-{$end}</b> &nbsp;&nbsp;{$cpage}/{$pagenum}";
    echo "&nbsp;&nbsp;<a href='{$url}?page=1'>首页</a>&nbsp;&nbsp";
    if($copge!=1)
    {
        echo "&nbsp;&nbsp;<a href='{$url}?page={$prev}'>上一页</a>&nbsp;&nbsp";
    }else{
        echo "&nbsp;&nbsp;<a href='{$url}?page=1'>上一页</a>&nbsp;&nbsp";
    }
    if($coage!=$pagenum){
        echo "&nbsp;&nbsp;<a href='{$url}?page={$next}'>下一页</a>&nbsp&nbsp";
    }else{
        echo "&nbsp;&nbsp<a href='{$url}?page={$pagenum}'>尾页</a>&nbsp;&nbsp";
    }
    echo "&nbsp;&nbsp<a href='{$url}?page={$pagenum}'>尾页</a>&nbsp;&nbsp";
    echo '</td></tr>';
    echo '</table>';




    mysql_free_result($result);
}
table('shops');
 //关闭连接
mysql_close(); 
?>