Home > Backend Development > PHP Tutorial > php 怎么同时输出 MySQL表的字段和值

php 怎么同时输出 MySQL表的字段和值

WBOY
Release: 2016-06-23 13:49:27
Original
1214 people have browsed it

  比如 test表     字段的自定义的 。。不是固定的

sid  name  age 
1      test       10
2      majia    123
…………              


    1
    test



    2
    majia


回复讨论(解决方案)

读取出来的数组是二维数组

//假如读取出来的值为$rowsforeach($rows as $row){echo '<ul>';  foreach($row as $key => $value){  echo "<$key>$value</$key>";  }echo '</ul>';}
Copy after login

    $mysql_server_name="localhost";        //数据库服务器名称    $mysql_username="dbuser";                //连接数据库用户名    $mysql_password="dbpw";                //连接数据库密码    $mysql_database="dbname";                //数据库的名字    $mysql_table="tablename";                //其中的一个表名    if (function_exists("mysql_close")) echo "PHP支持mysql"."<br /><br />";      else echo "PHP不支持mysql"."<br /><br />";    error_reporting(0);    // 连接到数据库    $conn=mysql_connect($mysql_server_name, $mysql_username,                        $mysql_password);    $err = mysql_error();    if($err) echo "$err";      else echo "mysql连接成功"."<br /><br />";        // 从表中提取信息的sql语句        //一定要找个表里有数据的,否则会有错误出现    $strsql="select * from ".$mysql_table;    // 执行sql查询    $result=mysql_db_query($mysql_database, $strsql, $conn);    // 获取查询结果    $row=mysql_fetch_row($result);        echo '<font face="verdana">';    echo '<table border="1" cellpadding="1" cellspacing="2">';    // 显示字段名称    echo "\n<tr>\n";    for ($i=0; $i<mysql_num_fields($result); $i++)    {      echo '<td bgcolor="white"><b>'.      mysql_field_name($result, $i);      echo "</b></td>\n";    }    echo "</tr>\n";    // 定位到第一条记录    mysql_data_seek($result, 0);    // 循环取出记录    while ($row=mysql_fetch_row($result))    {      echo "<tr>\n";      for ($i=0; $i<mysql_num_fields($result); $i++ )      {        echo '<td bgcolor="white">';        echo "$row[$i]";        echo '</td>';      }      echo "</tr>\n";    }        echo "</table>\n";    echo "</font>";    // 释放资源    mysql_free_result($result);    // 关闭连接    mysql_close(); 
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template