Accueil de Gladir.com Notes légales de Gladir.com Flux RSS des nouvelles du site Gladir.com - Langage de programmation - PHP - RecordSet Section du logiciel DOS «MonsterBook» Inventaire de la bibliothèque de Gladir.com Entrée administrateur

Il peut parfois être ardu de passer de la méthode de programmation des BD avec ASP vers PHP. Pour remedier à ce problème, on peut développer une classe effectuant sensiblement le même comportement. En prenant pour acquis que vous avez d'abord effectuer votre connexion avec une instruction de style comme celui-ci «$Conn = @mysql_pconnect();» avec MySQL par exemple, voici maintenant l'exemple en PHP montrant comment on crée une classe «RecordSet» compatible avec celui du langage de programmation Web ASP :

  1. class RecordSet {
  2.      var $result           = "";
  3.      var $Record       = array();
  4.      var $RecordCount = 0;
  5.      var $NumField      = 0;
  6.      var $Query_ID      = 0;
  7.      var $Instruction;
  8.      
  9.      function ExecuteRaw($Sql) {
  10.           global $Conn,$DBType;
  11.           switch ($DBType) {
  12.                case "OCI":
  13.                     $this->Line             = 0;
  14.                     $this->Instruction = ociparse($Conn,$Sql);
  15.                     $this->$result = ociexecute($this->Instruction);
  16.                     if(ocistatementtype($this->Instruction) == "SELECT") $this->RecordCount = ocifetchstatement($this->Instruction,$this->result);
  17.                                                                                else $this->RecordCount = ocirowcount($this->Instruction);
  18.                     $this->NumField     = ocinumcols($this->Instruction);
  19.                     break;
  20.                case "Oracle":
  21.                     $this->$result = ora_exec($Sql);
  22.                     break;
  23.                case "ODBC":
  24.                     $this->$result = odbc_exec($Conn,$Sql);
  25.                     break;
  26.                case "mysqli":
  27.                     $this->Query_ID = @mysqli_query($Conn,$Sql);
  28.                     $this->Error    = @mysqli_error($Conn);
  29.                     if($this->Error) {
  30.                          $this->RecordCount = 0;
  31.                          $this->NumField        = 0;
  32.                     } elseif (($this->Query_ID)&&(strpos(" ".strtoupper($Sql),"SELECT")) ) {
  33.                          $this->RecordCount = mysqli_num_rows($this->Query_ID);
  34.                          $this->NumField        = mysqli_num_fields($this->Query_ID);
  35.                     } else {
  36.                          $this->RecordCount = 0;
  37.                          $this->NumField        = 0;
  38.                     }
  39.                     return $this->Query_ID;
  40.                     break;
  41.                default:
  42.                     $this->Query_ID = @mysql_query($Sql);
  43.                     $this->Error    = @mysql_error();
  44.                     if($this->Error) {
  45.                          $this->RecordCount = 0;
  46.                          $this->NumField        = 0;
  47.                     }
  48.                     elseif (($this->Query_ID)&&((strpos(" ".strtoupper(substr(trim($Sql),0,10)),"SELECT")) || (strpos(" ".strtoupper(substr(trim($Sql),0,4)),"SHOW"))) ) {
  49.                          $this->RecordCount = mysql_num_rows($this->Query_ID);
  50.                          $this->NumField        = mysql_num_fields($this->Query_ID);
  51.                     } else {
  52.                          $this->RecordCount = 0;
  53.                          $this->NumField        = 0;
  54.                     }
  55.                     return $this->Query_ID;
  56.                     break;
  57.           }
  58.           return $this->$result;
  59.      }
  60.      
  61.      function NotEOF() {
  62.           global $DBType;
  63.           switch ($DBType) {
  64.                case "OCI":
  65.                     $this->Line++;
  66.                     return ($this->Line<=$this->RecordCount);
  67.                     break;
  68.                case "Oracle":
  69.                     $this->Record = ora_fetch($this->$Record);
  70.                     break;
  71.                case "ODBC":
  72.                     $this->Record = odbc_fetch_into($this->$Record);
  73.                     break;
  74.                case "mysqli":
  75.                     if($this->Error) return false;
  76.                     if($this->Query_ID) $this->Record = mysqli_fetch_array($this->Query_ID,MYSQLI_BOTH);
  77.                     break;
  78.                default:
  79.                     if($this->Error) return false;
  80.                     if($this->Query_ID) $this->Record = mysql_fetch_array($this->Query_ID);
  81.                     break;
  82.           }
  83.           return $this->Record;
  84.      }
  85.      
  86.      function Error() {
  87.           global $Conn,$DBType;
  88.           switch ($DBType) {
  89.                case "OCI":
  90.                     $error = OCIError($Conn);
  91.                     return $error["message"];
  92.                     break;
  93.                case "Oracle":
  94.                     return ora_error($this->$result);
  95.                     break;
  96.                case "ODBC":
  97.                     return odbc_errormsg($Conn);
  98.                     break;
  99.                default:
  100.                     return $this->Error;
  101.                     break;
  102.           }
  103.      }
  104.      
  105.      function f($Name) {
  106.           global $DBType;
  107.           switch ($DBType) {
  108.                case "OCI":
  109.                     if(is_int($Name)) {
  110.                          $col = each($this->result);
  111.                          reset($this->result);
  112.                          for($I=0;$I<=$Name;$I++) {
  113.                               $col = each($this->result);
  114.                               $data = $col["value"];
  115.                          }
  116.                          return $data[$this->Line-1];
  117.                     } else {
  118.                          return $this->result[strtoupper($Name)][$this->Line-1];
  119.                     }
  120.                     break;
  121.                default:
  122.                    if (isset($this->Record[$Name])) return $this->Record[$Name];
  123.                     break;
  124.          }
  125.      }
  126.      
  127.      function Reset() {
  128.           reset($this->Record);
  129.      }
  130.      
  131.      function TableName($No) {
  132.           global $DBType;
  133.           switch ($DBType) {
  134.                case "OCI":break;
  135.                case "Oracle":break;
  136.                case "ODBC":break;
  137.                default:return mysql_field_table($this->Query_ID,$No);break;
  138.           }
  139.      }
  140.      
  141.      function FieldName($No) {
  142.           global $DBType;
  143.           switch ($DBType) {
  144.                case "OCI":return ociColumnName($this->Instruction,$No+1);break;
  145.                case "Oracle":return ora_ColumnName($this->$result,$No);break;
  146.                case "ODBC":return odbc_field_name($this->$result,$No);break;
  147.                default:return @mysql_field_name($this->Query_ID,$No);break;
  148.           }
  149.      }
  150.      
  151.      function FieldType($No) {
  152.           global $DBType;
  153.           switch ($DBType) {
  154.                case "OCI":return ociColumnType($this->Instruction,$No+1);break;
  155.                case "Oracle":return ora_ColumnType($this->$result,$No);break;
  156.                case "ODBC":return odbc_field_type($this->$result,$No);break;
  157.                default:
  158.                     $Result = @mysql_field_type($this->Query_ID,$No);
  159.                     switch ($Result) {
  160.                          case "string":$Result = "VARCHAR";break;
  161.                     }
  162.                     return $Result;
  163.                     break;
  164.           }
  165.      }
  166.      
  167.      function FieldSize($No) {
  168.           global $DBType;
  169.           switch ($DBType) {
  170.                case "OCI":return ociColumnSize($this->Instruction,$No+1);break;
  171.                case "Oracle":return ora_ColumnSize($this->$result,$No);break;
  172.                case "ODBC":return odbc_field_len($this->$result,$No);break;
  173.                default:return @mysql_field_len($this->Query_ID,$No);break;
  174.           }
  175.      }
  176.      
  177.      function MoveNext() {
  178.           // Fonction bidon
  179.      }
  180.      
  181.      function CommitTrans() {
  182.           // Fonction bidon
  183.      }
  184.      
  185.      function RollBackTrans() {
  186.           // Fonction bidon
  187.      }
  188.      
  189.      function Close() {
  190.           global $DBType;
  191.           switch ($DBType) {
  192.                case "OCI":
  193.                     if ( $this->$result != null ) return ociFreeStatement($this->Instruction);
  194.                     break;
  195.                case "Oracle":
  196.                     if ( $this->$result != null ) return ora_close($this->$result);
  197.                     break;
  198.                case "ODBC":
  199.                     if ( $this->$result != null ) return odbc_free_result($this->$result);
  200.                     break;
  201.                case "mysqli":
  202.                     if($this->Query_ID ) {
  203.                          if($this->RecordCount>0) return mysqli_free_result($this->Query_ID);
  204.                          $this->RecordCount = 0;
  205.                     }
  206.                     break;
  207.                default:
  208.                     if ( $this->Query_ID ) {
  209.                          if($this->RecordCount>0) return mysql_free_result($this->Query_ID);
  210.                          $this->RecordCount = 0;
  211.                     }
  212.                     break;
  213.           }
  214.      }
  215. }

Cette solution n'est bien sûre qu'une solution de compatibilité dans un transition à court terme, si vous voulez vraiment effectuer une application professionnel, vous devrez sans doute envisager d'utiliser plutôt un ORM comme Doctrine ou Propel.


Dernière mise à jour: Lundi, le 19 octobre 2009