php读MySQL

来源:互联网 发布:淘宝网裙子款式 编辑:程序博客网 时间:2024/05/18 03:44

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> mysqlComplex.php </title>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
   <?php
    echo "This is a test</br>";
    echo "asdfasdfadsf";
    $mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="root"; // 连接数据库用户名
    $mysql_password=""; // 连接数据库密码
    $mysql_database="ForPhpTest"; // 数据库的名字
   
    // 连接到数据库
    $conn=mysql_connect($mysql_server_name, $mysql_username,
                        $mysql_password);
                       
     // 从表中提取信息的sql语句
    $strsql="SELECT * FROM testTable";
 /*
    // 执行sql查询
    $result=mysql_db_query($mysql_database, $strsql, $conn);
 */
mysql_select_db($mysql_database, $conn);
mysql_query('SET NAMES UTF8');
$result = mysql_query($strsql);


    // 获取查询结果
    $row=mysql_fetch_row($result);
   
    
    echo '<font face="verdana">';
    echo '<table border="1" cellpadding="1" cellspacing="2">';

    // 显示字段名称
    echo "</b><tr></b>";
    for ($i=0; $i<mysql_num_fields($result); $i++)
    {
      echo '<td bgcolor="#000F00"><b>'.
      mysql_field_name($result, $i);
      echo "</b></td></b>";
    }
    echo "</tr></b>";
    // 定位到第一条记录
    mysql_data_seek($result, 0);
    // 循环取出记录
    while ($row=mysql_fetch_row($result))
    {
      echo "<tr></b>";
      for ($i=0; $i<mysql_num_fields($result); $i++ )
      {
        echo '<td bgcolor="#00FF00">';
        echo $row[$i];
        echo '</td>';
      }
      echo "</tr></b>";
    }
  
    echo "</table></b>";
    echo "</font>";
    // 释放资源
    mysql_free_result($result);
    // 关闭连接
    mysql_close($conn); 
?>
 </body>
</html>

0 0