当前位置:首页 > PHP教程 > php应用 > 列表

php实现通用的从数据库表读取数据到数组的函数实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-17 15:34:04 浏览: 评论:0 

这篇文章主要介绍了php实现通用的从数据库表读取数据到数组的函数,实例分析了php实现通用的数据库查询技巧,非常具有实用价值,需要的朋友可以参考下。

本文实例讲述了php实现通用的从数据库表读取数据到数组的函数,分享给大家供大家参考,具体分析如下:

此函数不关心表结构,只需要指定表名、结构和查询条件既可以对表进行通用查询操作,非常实用。

  1. function listmytablerows($table, $name, $field, $where, $textID) 
  2. { 
  3. / / Connect to the database and query execution 
  4. connect (); 
  5. $Sql = "select * from". $table. "". $where. "ORDER BY". $field; 
  6. $Req = mysql_query($sql); 
  7. $Res = mysql_num_rows($req); 
  8. ?> 
  9. <Select name = "<?php echo $name; ?>" id="<?php echo $textID; ?>"> 
  10. <option value=""> ____</ option> 
  11. <? Php 
  12. / / We do a loop that will read the information for each record 
  13. while ($data = mysql_fetch_array($res)) 
  14. { 
  15.   / / We display the information from the current record 
  16. ?> 
  17.    <Option value = "<?php echo $data['id']; ?>"> 
  18.    <?php echo $data[$field]; ?> 
  19. </ Option> 
  20.    <? Php 
  21.   } 
  22. ?> 
  23. </ Select> 
  24. <? Php 
  25. } 
  26. ?>

Tags: php读取数据

分享到: