Home > php教程 > php手册 > PHP将MYSQL内容读到二维数组并按指定列输出

PHP将MYSQL内容读到二维数组并按指定列输出

WBOY
Release: 2016-06-21 09:01:32
Original
1149 people have browsed it

  PHP将MYSQL内容读到二维数组并按指定列输出

<?
$host = "localhost";   //主机名
$user = "root";        //mysql用户名
$password = "";    //mysql密码
$database = "doc";  //mysql数据库名
$tables = "mclass";  //表名

$conn=mysql_connect("$host","$user","$password") or die("数据库打开出错");
mysql_select_db("$database");

   $query="select * from $tables";
    $result=mysql_query($query,$conn);
    $i=0;
    $j=0;
      while($row=mysql_fetch_row($result)){
      $array[$i][$j] = $row[0].$row[1];
        $array2[$i][$j] = $row[0];
     $j++; // echo $i.",".$j.$row[1]."<br>";
     if($j==3) {
     $i++;
     $j=0;
         } //else {$j++ ;}

       }
     $amax=count($array);//获是数组内数据个数。

    $rows=2;  //设置列数

   //开始显示数据
  for ($x=0; $x<=$amax-1; $x++) {
       for ($y=0; $y<= $rows; $y++) {
         echo "<a href=".$array2[$x][$y].">".$array[$x][$y]."</a>|" ;
          }
          echo "<p>";
    }

/*
显示结果为
aaa|bbb|ccc
ddd|eee|fff
lll|mmm|nnn
.....
*/

?>



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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template