Kenny 2007-8-1 22:36
将数据库的内容读到二维数组并按指定列输出
<p><P><STRONG>将<a href="http://www.phpchina.com/javascript:;" onClick="javascript:tagshow(event, '%CA%FD%BE%DD%BF%E2');" target="_self"><u><strong>数据库</strong></u></a>的内容读到二维数组并按指定列输出</STRONG> </P><P><? <BR>$host = "localhost"; //主机名 <BR>$user = "root"; //mysql用户名 <BR>$password = ""; //mysql密码 <BR>$database = "doc"; //mysql数据库名 <BR>$tables = "mclass"; //表名 <BR><BR>$conn=mysql_connect("$host","$user","$password") or die("数据库打开出错"); <BR>mysql_select_db("$database"); <BR><BR> $query="select * from $tables"; <BR> $result=mysql_query($query,$conn); <BR> $i=0; <BR> $j=0; <BR> while($row=mysql_fetch_row($result)){ <BR> $array[$i][$j] = $row[0].$row[1]; <BR> $array2[$i][$j] = $row[0]; <BR> $j++; // echo $i.",".$j.$row[1]."<br>"; <BR> if($j==3) { <BR> $i++; <BR> $j=0; <BR> } //else {$j++ ;} <BR><BR> } <BR> $amax=count($array);//获是数组内数据个数。 <BR><BR> $rows=2; //设置列数 <BR><BR> //开始显示数据 <BR> for ($x=0; $x<=$amax-1; $x++) { <BR> for ($y=0; $y<= $rows; $y++) { <BR> echo "<a href="http://www.phpchina.com/.$array2[$x][$y].">".$array[$x][$y]."</a>|" ; <BR> } <BR> echo "<p>"; <BR> } <BR><BR>/* <BR>显示结果为 <BR>aaa|bbb|ccc <BR>ddd|eee|fff <BR>lll|mmm|nnn <BR>..... <BR>*/ <BR><BR>?> <BR></P></p> <center><input type="image" onclick=copyToClipBoard() src="http://www.phpchina.com/images/phpcn_book_bu_tj.gif" border="0"></center>