Chinaunix首页 | 论坛 | 博客
  • 博客访问: 292893
  • 博文数量: 68
  • 博客积分: 1474
  • 博客等级: 上尉
  • 技术积分: 616
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-12 12:07
文章分类

全部博文(68)

文章存档

2011年(68)

分类: 系统运维

2011-10-09 17:23:11

  1. <?php
  2. class Mysql{
  3. private $HOST = "localhost"; // 数据库地址
  4. private $USER = "root"; // 数据库帐号
  5. private $PASS = "lizhihua"; // 数据库密码
  6. private $DBNAME = "db_iqoomea"; // 数据库库名
  7. private $CONN;
  8. /**
  9. * 构造函数:连接数据库
  10. * @return TRUE:连接成功;FALSE:连接失败。
  11. */
  12. function Mysql() {
  13.    $user = $this->USER;
  14.    $pass = $this->PASS;
  15.    $host = $this->HOST;
  16.    $db = $this->DBNAME;
  17.    // 连接数据库
  18.    $conn = mysql_connect($host, $user, $pass);
  19.    mysql_select_db($db, $conn);
  20.    mysql_query("SET NAMES UTF8");
  21.    $this->CONN = $conn;
  22.    return true;
  23. }
  24. /**
  25. * 数据库表查询
  26. *如 select * from table
  27. */
  28. function getallinfo($table) {
  29.    if ( empty($table)) return false;
  30.    if ( empty($this->CONN) ) return false;
  31.    $conn = $this->CONN;
  32.    // 发送SQL语句,获得结果
  33.    $sql = "select * from $table";
  34.    $result = mysql_query($sql, $conn);
  35.    if ( (!$result) or (empty($result)) ) {
  36.     return false;
  37.    }
  38.    $num = 0;
  39.    $data = array();
  40.    // 将查询结果放二维数组中
  41.    while ( ($row = mysql_fetch_array($result)) ) {
  42.     $data[$num] = $row;
  43.     $num++;
  44.    }
  45.    mysql_free_result($result);
  46.    return $data;
  47. }

  48. //可用于插入、修改、更新、删除数据,创建数据表
  49. function query($strSQL) {
  50.    if ( empty($strSQL) ) return false;
  51.    if ( empty($this->CONN) ) return false;
  52.    $conn = $this->CONN;
  53.    $result = mysql_query($strSQL,$conn);
  54.    if ( !result ) return false;
  55.    return $result;
  56. }

  57. function select($table,$field,$findname){
  58.     if ( empty($table) ) return false;
  59.     if ( empty($field) ) return false;
  60.     if ( empty($findname) ) return false;
  61.     if ( empty($this->CONN) ) return false;
  62.     $conn = $this->CONN;
  63.     $sql = "select * from $table where $field='$findname'";
  64.     $result = mysql_query($sql, $conn);
  65.     if ( (!$result) or (empty($result)) ) {
  66.         return false;
  67.     }
  68.     $data = mysql_fetch_array($result);
  69.     mysql_free_result($result);
  70.     return $data;
  71. }

  72. //根据查询条件,返回查询单条记录
  73. function getinfo($strSQL) {
  74.    if ( empty($strSQL) ) return false;
  75.    if ( empty($this->CONN) ) return false;
  76.    $conn = $this->CONN;
  77.    // 发送SQL语句,获得结果
  78.    $result = mysql_query($strSQL, $conn);
  79.    if ( (!$result) or (empty($result)) ) {
  80.     return false;
  81.    }
  82.    $data = mysql_fetch_array($result);
  83.    mysql_free_result($result);
  84.    return $data;
  85. }

  86. //根据查询条件,返回多条记录
  87. function getmoreinfo($strSQL){
  88.    if ( empty($strSQL) ) return false;
  89.    if ( empty($this->CONN) ) return false;
  90.    $conn = $this->CONN;
  91.    $result = mysql_query($strSQL, $conn);
  92.    if ( (!$result) or (empty($result)) ) {
  93.     return false;
  94.    }
  95.    $num = 0;
  96.    $data = array();
  97.    // 将查询结果放二维数组中
  98.    while ( ($row = mysql_fetch_array($result)) ) {
  99.     $data[$num] = $row;
  100.     $num++;
  101.    }
  102.    mysql_free_result($result);
  103.    return $data;
  104. }
  105. //根据查询条件,返回多条记录
  106. //function getallinfo($table){
  107. // if ( empty($table) ) return false;
  108. // if ( empty($this->CONN) ) return false;
  109. // $conn = $this->CONN;
  110. // $sql = "select * from $table";
  111. // $result = mysql_query($sql, $conn);
  112. // if ( (!$result) or (empty($result)) ) {
  113. // return false;
  114. // }
  115. // $num = 0;
  116. // $data = array();
  117. // // 将查询结果放二维数组中
  118. // while ( ($row = mysql_fetch_array($result)) ) {
  119. // $data[$num] = $row;
  120. // $num++;
  121. // }
  122. // mysql_free_result($result);
  123. // return $data;
  124. //}

  125. //返回影响(插入,查询,更新,删除)到的行数
  126. function getrowsnum($strSQL){
  127.    if ( empty($strSQL) ) return false;
  128.    if ( empty($this->CONN) ) return false;
  129.    $conn = $this->CONN;
  130.    $result = mysql_query($strSQL,$conn);
  131.    $num = mysql_num_rows($result);
  132.    mysql_free_result($result);
  133.    return $num;
  134. }

  135. function getallrowsnum($table){
  136.    if ( empty($table) ) return false;
  137.    if ( empty($this->CONN) ) return false;
  138.    $conn = $this->CONN;
  139.    $sql = "select * from $table";
  140.    $result = mysql_query($sql,$conn);
  141.    $num = mysql_num_rows($result);
  142.    mysql_free_result($result);
  143.    return $num;
  144. }

  145. /* 可用于修改数据
  146. * $tableName 表名
  147. * $setPorpertyName 修改的字段名
  148. * $setValue 修改的值
  149. * $id 指定id
  150. *
  151. */
  152. function update($strSQL) {
  153.    if ( empty($strSQL) ) return false;
  154.    if ( empty($this->CONN) ) return false;
  155.    $conn = $this->CONN;
  156.    // 发送SQL语句,更新数据库
  157.    $result = mysql_query($strSQL, $conn);
  158.    if(!$result)return false;
  159.    return $result;
  160. }

  161. //删除指定表的某个id的记录
  162. function delete($tableName,$id) {
  163.    if ( empty($tableName) ) return false;
  164.     if ( empty($this->CONN) ) return false;
  165.     $conn = $this->CONN;
  166.     $strSQL="delete from $tableName where id='$id'";
  167.     $result = mysql_query($strSQL, $conn);
  168.     return true;
  169. }

  170. //返回一个数组,数组包含该表所以字段
  171. function getfields($table){
  172.     if ( empty($table) ) return false;
  173.     if ( empty($this->CONN) ) return false;
  174.     $conn = $this->CONN;
  175.     $sql = "show full fields from $table";
  176.     $showresult = mysql_query($sql,$conn);
  177.     $showdata = array();
  178.     while($showrow = mysql_fetch_array($showresult)){
  179.         $showdata[] = $showrow['Field'];
  180.     }
  181.     mysql_free_result($showresult);
  182.     return $showdata;
  183. }
  184. //返回一个表的字段的数目
  185. function getfieldsnum($table){
  186.     if ( empty($table)) return false;
  187.     if ( empty($this->CONN) ) return false;
  188.     $conn = $this->CONN;
  189.     $sql = "select * from $table";
  190.     $showresult = mysql_query($sql,$conn);
  191.     $getcount = mysql_num_fields($showresult);
  192.     mysql_free_result($showresult);
  193.     return $getcount;
  194. }

  195. }
  196. ?>
使用举例:

  1. <?php
  2. header("content-Type: text/html; charset=utf-8");
  3. session_start();
  4. include "../include/conn.inc";
  5. $str=microtime();
  6. $arr=explode(" ",$str);
  7. $start = $arr[0] * 1000;
  8. $consql = new Mysql();
  9. mysql_query('SET NAMES utf8;');
  10. $valuetype = 2;

  11. $musicname = $_GET['musicname'];

  12. //$sqlsec = "select * from SecondDay_Info where CityName='$musicname'";

  13. $Song_Info = array();
  14. $Singer_Info = array();
  15. $Album_Info = array();
  16. $SecondDay_Info = array();
  17. $strSQL = "select * from liricstable where singer_name='$musicname'";
  18. if($valuetype == 2){
  19.     $Song_Info = $consql->getmoreinfo($strSQL);
  20.     print_r($str1);
  21.     foreach($Song_Info as $rows){
  22.         $str = $rows['song_name'] . "----" . $rows['singer_name'] . "----" . $rows['album_name'] . "----" . $rows['liric_path'] . "----" . $rows[album_cover_path] . "\n";
  23.         print_r($str);
  24.     }
  25. }
  26. ?>
阅读(2105) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~