PHP中类似ASP中的recordcount

create database victor

use victor

create table mytable(id int primary key auto_increment,username char(10))

insert into mytable(username) values('victor'),('test'),('ysy'),('siyuroom')

<?php
$hostName = "localhost";
$userName = "root";
$password = "115115";
$dbName = "victor";
$cn=mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
$db=mysql_select_db($dbName) or die( "Unable to select database $dbName");
$query = "SELECT *FROM mytable";
$result = mysql_query($query);
$number = mysql_num_rows($result);    //取得符合条件的所有记录条数
print "There are $number types of computers:<p>";
for ($i=0; $i<$number; $i++) {
     $username = mysql_result($result,$i,"username");    
     print "$username<br>";
}
echo mysql_result($result,5,1);         显示第5条,字段为表中的第二个字段,默认为第一个字段
echo mysql_result($result,5,"username");  同上
mysql_close();
?>

mysql_fetch_object

<?php
mysql_connect("localhost", "root", "115115");
mysql_select_db("victor");
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result)) {
    echo $row->id;
    echo $row->username;
}
mysql_free_result($result);
?>

 

    A+
发布日期:2007年08月26日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: