php获取mysql表中所有数据,实例代码如下:
<?php
$user = \"root\";
$pass = \"\";
$db = \"test\";
$ = mysql_connect( \"localhost\", $user, $pass );
if ( ! $ ) {
die( \"Couldn\'t connect to MySQL: \".mysql_error() );
}
mysql_select_db( $db, $ ) or die ( \"Couldn\'t open $db: \".mysql_error() );
$result = mysql_query( \"SELECT * FROM student\" );
$num_rows = mysql_num_rows( $result );
print \"<p>表中有 $num_rows 条数据</p>\\n\";
print \"<table>\";
while ( $a_row = mysql_fetch_row( $result ) ) {
print \"<tr>\";
foreach ( $a_row as $field ) {
print \"<td>\".stripslashes($field).\"</td>\";
}
print \"</tr>\";
}
print \"</table>\";
mysql_close( $ );
?>
代码解释:
- mysql_connect用于连接数据库服务器。
- mysql_select_db选择需要操作的数据库名称。
- mysql_query执行SQL语句,本实例中执行“SELECT * FROM student”表示查询student表中的所有数据,如果执行成功,则返回一个资源标识符,该资源标识符将在下面解析数据会用到。
- mysql_num_rows获取查询结果集有多少条数据。mysql_num_rows会接受一个资源标识符,该资源标识符由mysql_query所返回的。
- mysql_num_rows用于解析每一行数据。
继续阅读与本文标签相同的文章
上一篇 :
2019年非常受欢迎的9个超级云原生开源项目
-
如果 5G 覆盖以后,是否可以将处理器从手机上撤掉?
2026-05-14栏目: 教程
-
Python实现文件目录的创建、修改及删除
2026-05-14栏目: 教程
-
Excel双色图表,年终总结用得着
2026-05-14栏目: 教程
-
苹果和Facebook竟因为一处房产而掐架
2026-05-14栏目: 教程
-
Windows 10 1909预计推送时间为11月12日
2026-05-14栏目: 教程
