脚本宝典收集整理的这篇文章主要介绍了php – 显示mysql表中的所有记录,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
I.E.:字段名称作为htML表中每列的标题和标题下方的条目.希望这对某人有意义;)
$raw = MysqL_query("SELECT * From tbl_gas_meters"); $allresults = MysqL_fetch_array($raw); $field = MysqL_query("SELECT * From tbl_gas_meters"); $num_fields = MysqL_num_fields($raw); $num_rows = MysqL_num_rows($raw); $i = 1; PRint "<table border=1>\n"; while ($i < $num_fields) { echo "<tr>"; echo "<b><td>" . MysqL_field_name($field,$i) . "</td></b>"; //echo ": "; echo '<td><font color ="red">' . $allresults[$i] . '</font></td>'; $i++; echo "</tr>"; //echo "<br>"; } print "</table>";
http://www.php.net/manual/en/intro.pdo.php
此示例使用对象而不是数组.不一定重要,但它使用较少的字符,所以我喜欢它.当您深入了解对象时,差异确实存在,但在此示例中则不然.
//connection information $user = "your_MysqL_user"; $pass = "your_MysqL_user_pass"; $dbh = new PDO('MysqL:host=your_hostname;dbname=your_db;charset=UTF-8',$user,$pass); //prepare statement to query table $sth = $dbh->prepare("SELECT name,colour FROM fruIT"); $sth->execute(); //loop over all table rows and fetch them as an object while($result = $sth->fetch(PDO::FETCH_OBJ)) { //print out the fruits name in this case. print $result->name; print("\n"); print $result->colour; print("\n"); }
您可能还想查看准备好的语句.这有助于防止注射.出于安全原因,注射是不好的.这是该页面.
http://www.php.net/manual/en/pdostatement.bindparam.php
您可能也应该考虑清理用户输入.只是抬头而且与你目前的情况无关.
$q = $dbh->prepare("DESCRIBE tablename"); $q->execute(); $table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
一旦你拥有了所有的表字段,使用< div>就可以了.甚至是< table>使用< th>来安排它们.
快乐学习PHP.很好玩.
以上是脚本宝典为你收集整理的php – 显示mysql表中的所有记录全部内容,希望文章能够帮你解决php – 显示mysql表中的所有记录所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。