PHP查询数据函数(转自网络)

来源:互联网 发布:linux exec命令符号 编辑:程序博客网 时间:2024/05/21 06:45

 

<?php

/*
 PHP查询数据函数:
 mysql_result()
 mysql_fetch_row()
 mysql_fetch_array() 
 mysql_fetch_assoc()
 mysql_fetch_object() 
*/
$conn=mysql_connect("localhost","root","");//连接数据库
mysql_select_db("test",$conn);//选择数据库test
//查询数据
$query="SELECT id,name,gender,phone,email FROM student";
mysql_query("set names 'gbk' ");
$result=mysql_query($query);

/*
// mysql_fetch_row() 函数用法一
while($rows=mysql_fetch_row($result))
{
    echo "学号:".$rows[0]."<br>";
    echo "姓名:".$rows[1]."<br>";
    echo "性别:".$rows[2]."<br>";
    echo "电话:".$rows[3]."<br>";
    echo "E-mail:".$rows[4]."<br>";
    echo "<br>";
}
*/

/*
//---------------------------------------------------------------------------
// mysql_fetch_row() 函数用法二
while(list($id,$name,$gender,$phone,$email)=mysql_fetch_row($result))
{
    echo "学号:".$id."<br>";
    echo "姓名:".$name."<br>";
    echo "性别:".$gender."<br>";
    echo "电话:".$phone."<br>";
    echo "E-mail:".$email."<br>";
    echo "<br>";
}
*/

//------------------------------------------------------------------------------

/*
// mysql_fetch_array() 函数用法
while($rows=mysql_fetch_array($result))
{
    echo "学号:".$rows['id']."<br>";
    echo "姓名:".$rows['name']."<br>";
    echo "性别:".$rows['gender']."<br>";
    echo "电话:".$rows['phone']."<br>";
    echo "<br>";
}
*/
//------------------------------------------------------------------------------

/*
// mysql_fetch_assoc() 函数用法
while($rows=mysql_fetch_assoc($result))
{
    echo "学号:".$rows['id']."<br>";
    echo "姓名:".$rows['name']."<br>";
    echo "性别:".$rows['gender']."<br>";
    echo "电话:".$rows['phone']."<br>";
    echo "<br>";
}
*/

//-----------------------------------------------------------------

// mysql_fetch_object() 函数用法

//以对象的形式返回查询结果

while($rows=mysql_fetch_object($result))
{
    echo "学号:".$rows->id."<br>";
    echo "姓名:".$rows->name."<br>";
    echo "性别:".$rows->gender."<br>";
    echo "电话:".$rows->phone."<br>";
    echo "E-mail:".$rows->email."<br>";
    echo "<br>";
}

?>

原创粉丝点击