MySQL Class

一个MySQL操作类
  1. <?
  2.  /***************************************************************************
  3. * Mysql.Class.php
  4. * -------------------------
  5. * begin : Thursday, Apr 27, 2006
  6. * copyright : (C) 2002 The ReQi.com
  7. * email : maxflg@yahoo.com
  8. *
  9. * $Id: Mysql.Class.php,v 1.0.2.1 2006/04/27 16:23:20 Syber Exp $
  10. *
  11. ***************************************************************************/
  12.  
  13.  /***************************************************************************
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. ***************************************************************************/
  21.  
  22.  /***************************************************************************
  23. *
  24. * Thanks my love,my all,my darling.
  25. *
  26. ***************************************************************************/
  27.  
  28.  
  29.  /***************************************************************************
  30. *
  31. * Example:
  32. * $mysql = new MySql_Class( 'dbuser', 'dbpassword', 'dbname', 'dbhost' );
  33. * $mysql -> DBConnect();
  34. * $mysql -> Post( 'UPDATE $table SET $column = $value WHERE $Expr' );
  35. * $row = $mysql -> Open( 'SELECT $columns FROM $table WHERE $Expr' );
  36. * $rows = $mysql -> Open( 'SELECT $columns FROM $table WHERE $Expr', True, True );
  37. *
  38. ***************************************************************************/
  39.  
  40.  
  41.  Class MySql_Class
  42.  {
  43.  
  44.  var $Conn;
  45.  var $DbUser;
  46.  var $DbPass;
  47.  var $DbName;
  48.  var $DbHost;
  49.  var $DbResult;
  50.  var $row = array();
  51.  var $rowset = array();
  52.  var $num_queries = 0;
  53.  
  54.  Function MySql_Class( $user, $passwd, $db, $host )
  55.  {
  56.  $this -> DbUser = $user;
  57.  $this -> DbPass = $passwd;
  58.  $this -> DbName = $db;
  59.  $this -> DbHost = $host;
  60.  } // end Function
  61.  
  62.  //
  63.  // Constructor
  64.  //
  65.  Function DBConnect( $persistency = false )
  66.  {
  67.  if( $persistency )
  68.  {
  69.  $this -> Conn = @mysql_pconnect( $this -> DbHost, $this -> DbUser, $this -> DbPass );
  70.  }
  71.  else
  72.  {
  73.  $this->Conn = @mysql_connect( $this -> DbHost, $this -> DbUser, $this -> DbPass );
  74.  }
  75.  
  76.  if( $this -> Conn )
  77.  {
  78.  if( DbName != "" )
  79.  {
  80.  $dbselect = @mysql_select_db( $this -> DbName );
  81.  if( !$dbselect )
  82.  {
  83. @mysql_close( $this -> Conn );
  84.  $this -> Conn = $dbselect;
  85.  }
  86.  }
  87.  return $this -> Conn;
  88.  }
  89.  else
  90.  {
  91.  return false;
  92.  }
  93.  } // end Function
  94.  
  95.  Function DBClose()
  96.  {
  97.  if( $this -> Conn )
  98.  {
  99.  if( $this -> DbResult )
  100.  {
  101. @mysql_free_result( $this -> DbResult );
  102.  }
  103.  $result = @mysql_close( $this -> Conn );
  104.  return $result;
  105.  }
  106.  else
  107.  {
  108.  return false;
  109.  }
  110.  } // end Function
  111.  
  112.  //
  113.  // Base query method
  114.  // When $Affectedrows = true,then return Affectedrows
  115.  //
  116.  Function Post( $sql, $Affectedrows = false )
  117.  {
  118.  unset( $this -> DbResult );
  119.  if( $sql != "" )
  120.  {
  121.  $this -> num_queries++;
  122.  $this -> DbResult = @mysql_query( $sql, $this -> Conn );
  123.  }
  124.  if( $this -> DbResult )
  125.  {
  126.  unset( $this -> row[ $this -> DbResult ] );
  127.  unset( $this -> rowset[ $this -> DbResult ] );
  128.  return $Affectedrows ? $this -> AffectedRows() : $this -> DbResult;
  129.  }
  130.  else
  131.  {
  132.  return false;
  133.  }
  134.  } // end Function
  135.  
  136.  //
  137.  // Base query method
  138.  // $MuchRows, get one row of false, much rows of true.
  139.  // $OutputFetch, get field row('s) of true.
  140.  //
  141.  Function Open( $sql, $MuchRows = false, $OutputFetch = false )
  142.  {
  143.  if ( $this -> Post( $sql ) )
  144.  {
  145.  if ( $MuchRows )
  146.  {
  147.  if ( $OutputFetch )
  148.  {
  149.  return $this -> SQL_FetchRowSet();
  150.  }
  151.  else
  152.  {
  153.  return $this -> SQL_ArrayRowSet();
  154.  }
  155.  }
  156.  else
  157.  {
  158.  if ( $OutputFetch )
  159.  {
  160.  return $this -> SQL_FetchRow();
  161.  }
  162.  else
  163.  {
  164.  return $this -> SQL_ArrayRow();
  165.  }
  166.  }
  167.  }
  168.  else
  169.  {
  170.  return false;
  171.  }
  172.  } // end Function
  173.  
  174.  Function SQL_FetchRow( $query_id = 0 )
  175.  {
  176.  if( !$query_id )
  177.  {
  178.  $query_id = $this -> DbResult;
  179.  }
  180.  if( $query_id )
  181.  {
  182.  $this -> row[ $query_id ] = @mysql_fetch_array( $query_id );
  183.  return $this -> row[ $query_id ];
  184.  }
  185.  else
  186.  {
  187.  return false;
  188.  }
  189.  } // end Function
  190.  
  191.  Function SQL_FetchRowSet( $query_id = 0 )
  192.  {
  193.  if( !$query_id )
  194.  {
  195.  $query_id = $this -> DbResult;
  196.  }
  197.  if( $query_id )
  198.  {
  199.  unset( $this -> rowset[ $query_id ] );
  200.  unset( $this -> row[ $query_id ] );
  201.  while( $this -> rowset[ $query_id ] = @mysql_fetch_array( $query_id ) )
  202.  {
  203.  $result[] = $this->rowset[ $query_id ];
  204.  }
  205.  return $result;
  206.  }
  207.  else
  208.  {
  209.  return false;
  210.  }
  211.  } // end Function
  212.  
  213.  Function SQL_ArrayRow( $query_id = 0 )
  214.  {
  215.  if( !$query_id )
  216.  {
  217.  $query_id = $this -> DbResult;
  218.  }
  219.  if( $query_id )
  220.  {
  221.  $this -> row[ $query_id ] = @mysql_fetch_row( $query_id );
  222.  return $this -> row[ $query_id ];
  223.  }
  224.  else
  225.  {
  226.  return false;
  227.  }
  228.  } // end Function
  229.  
  230.  Function SQL_ArrayRowSet( $query_id = 0 )
  231.  {
  232.  if( !$query_id )
  233.  {
  234.  $query_id = $this -> DbResult;
  235.  }
  236.  if( $query_id )
  237.  {
  238.  unset( $this -> rowset[ $query_id ] );
  239.  unset( $this -> row[ $query_id ] );
  240.  while( $this -> rowset[ $query_id ] = @mysql_fetch_row( $query_id ) )
  241.  {
  242.  $result[] = $this->rowset[ $query_id ];
  243.  }
  244.  return $result;
  245.  }
  246.  else
  247.  {
  248.  return false;
  249.  }
  250.  } // end Function
  251.  
  252.  Function AffectedRows()
  253.  {
  254.  if( $this -> Conn )
  255.  {
  256.  $result = @mysql_affected_rows( $this -> Conn );
  257.  return $result;
  258.  }
  259.  else
  260.  {
  261.  return false;
  262.  }
  263.  } // end Function
  264.  
  265.  Function SQL_NextID()
  266.  {
  267.  if( $this -> Conn)
  268.  {
  269.  $result = @mysql_insert_id( $this -> Conn );
  270.  return $result;
  271.  }
  272.  else
  273.  {
  274.  return false;
  275.  }
  276.  } // end Function
  277.  
  278.  Function SQL_FreeResult( $query_id = 0 )
  279.  {
  280.  if( !$query_id )
  281.  {
  282.  $query_id = $this -> DbResult;
  283.  }
  284.  
  285.  if ( $query_id )
  286.  {
  287.  unset( $this -> row[ $query_id ] );
  288.  unset( $this -> rowset[ $query_id ] );
  289.  
  290. @mysql_free_result( $query_id );
  291.  
  292.  return true;
  293.  }
  294.  else
  295.  {
  296.  return false;
  297.  }
  298.  } // end Function
  299.  
  300.  Function SQL_Error($query_id = 0)
  301.  {
  302.  $result[ "message" ] = @mysql_error( $this -> Conn);
  303.  $result[ "code" ] = @mysql_errno( $this -> Conn);
  304.  
  305.  return $result;
  306.  } // end Function
  307.  
  308.  } // end Class
  309.  ?>
引用通告地址: 点击获取引用地址
评论: 0 | 引用: 0 | 阅读: 169 | 打印 | 打包 | 转发
 加入网摘