php高手来帮忙,mysql怎么用一句代码输出数组数据?

php高手来帮忙,mysql怎么用一句代码输出数组数据?,第1张

这很简单的 我模拟下你的数据

//二维数组模拟你的数据库 相当于mysql_fetch_array($result)查出来的数据,当然查出来是一维的 我们现在把他格式化为二维

<html>

<head>

<?php

$table[]=array(

"user"=>"aaaa",

"count"=>"22",

"timeset"=>"1310344207",

"id"=>"1"

)

$table[]=array(

"user"=>"bbbb",

"count"=>"33",

"timeset"=>"1310344208",

"id"=>"2"

)

</head>

<body>

<table width="300" align="center">

<tr>

<td>用户名<td>

<td>登录次数<td>

<td>登录时间<td>

<td>id<td>

</tr>

<?php foreach($table as $value) { ?>

<tr>

<td><?= $value['user']?><td>

<td><?=$value['count']?><td>

<td><?=$value['timeset']?><td>

<td><?=$value['id']?><td>

</tr>

<?php } ?>

</table>

</body>

</html>

是不是你想要的结果?

<? 

$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 

..... 

*/

?>

首先定义一个数组,然后遍历数据表,把相应的数据放到数组中,最后通过json_encode()转化数组

json_encode() 函数的功能是将数值转换成json数据存储格式。

例如:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8635031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存