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

php 如何同时输出 MySQL表的字段和值

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:06:251185browse

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

本帖最后由 kj8629 于 2014-09-17 09:17:46 编辑   比如 test表     字段的自定义的 。。不是固定的

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


    1
    test



    2
    majia

------解决思路----------------------
读取出来的数组是二维数组
<br /><br />//假如读取出来的值为$rows<br />foreach($rows as $row){<br />echo '<ul>';<br />  foreach($row as $key => $value){<br />  echo "<$key>$value</$key>";<br />  }<br />echo '</ul>';<br />}<br /><br />

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