方法一:
if(FALSE == $link)
{
echo "Connect database failed!";
}
$query =
"select * from a order by id";
mysql_select_db('test', $link);--选择数据库
$result = mysql_query($query);--执行查询语句
if(FALSE == $result)
echo "Querry failed!";
$i = 0;
$j = 0;
while($i++ mysql_num_rows($result))--取总行数
$meta_c=0;
if($meta_c=mysql_fetch_row($result))--取每一行的结果集
while($j mysql_num_fields($result))--取一行的列数
echo $meta_c[$j;
echo "
";
} // while;
$j=0;
mysql_free_result($result);--释放结果集
mysql_close($link);--关闭连接
方法二:
echo "Connect database failed!
$result = mysql_query("select * from a order by id");--执行查询语句
while($row = mysql_fetch_array($result, MYSQL_NUM))--取数组里面的值,因为结果集以二维数组存放
foreach ($row as $col_value)--一一取值
echo " $col_value ";
mysql_close($link);)--关闭连接
php
$db=mysql_connect( 'localhost', '用户名', '密码' );
mysql_select_db('test',$db);
$query=mysql_query("select * from test");
while($rs=mysql_fetch_array($query))
table
tr
td?php echo $rs['id']?/td
td?php echo $rs['title']?/td
td?php echo $rs['content']?/td
/tr
/table
第一段:首先你需要在 $result=mysql_query($sql); 这一行之后取一下数据:
$row = mysql_fetch_array($result);
具体原因请看我以前回答过的问题:
第二段:之后查看获取到的数据:
① 查看具体值:
echo $result[0];
print_r($row);
var_dump($row);
//?这是因为你从资源型结果集中获取数据时只获取了一次,?如果查询为多条数据应该迭代资源型结果集
$r?=?mysql_query($sql);?//?你的结果集
$result?=?[];
while?(true)?{
$ary?=?mysql_fetch_assoc($r);?//?取出第一条数据,?数据指针向后移动一位
if?($ary)?{
$result[]?=?$ary;?//?存储到结果数组中
}?else?{
break;?//?如果取出的结果为false,?则代表数据获取完毕,?终止循环
echo?'pre';
print_r($result);?//?打印最终结果
echo?'/pre';
php 把数据导出到excel表格有多种方法,比如使用 phpExcel 等,以下代码是直接通过 header 生成 excel 文件的代码示例:
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=xls_region.xls");
$cfg_dbhost?=?'localhost';
$cfg_dbname?=?'testdb';
$cfg_dbuser?=?'root';
$cfg_dbpwd?=?'root';
//?END?配置
//链接数据库
$link?=?mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//选择编码
mysql_query("set?names?".$cfg_db_language);
//users表
$sql?=?"desc?users";
$res?=?mysql_query($sql);
echo?"tabletr";
//导出表头(也就是表中拥有的字段)
while($row?=?mysql_fetch_array($res)){
$t_field[]?=?$row['Field'];?//Field中的F要大写,否则没有结果
echo?"th".$row['Field']."/th";
echo?"/tr";
//导出100条数据
$sql?=?"select?*?from?users?limit?100";
echo?"tr";
foreach($t_field?as?$f_key){
echo?"td".$row[$f_key]."/td";
echo?"/table";
您好,PHP输出数据有四种方式,echo、var_dump、print_r、retrun(这个一般是PHP框架常用)
以上就是土嘎嘎小编为大家整理的php原生输出数据相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!