Il peut parfois être ardu de passer de la méthode de programmation des BD avec ASP vers PHP. Pour remédier à 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 :
- class RecordSet {
- var $result = "";
- var $Record = array();
- var $RecordCount = 0;
- var $NumField = 0;
- var $Query_ID = 0;
- var $Instruction;
-
- function ExecuteRaw($Sql) {
- global $Conn,$DBType;
- switch ($DBType) {
- case "OCI":
- $this->Line = 0;
- $this->Instruction = ociparse($Conn,$Sql);
- $this->$result = ociexecute($this->Instruction);
- if(ocistatementtype($this->Instruction) == "SELECT") $this->RecordCount = ocifetchstatement($this->Instruction,$this->result);
- else $this->RecordCount = ocirowcount($this->Instruction);
- $this->NumField = ocinumcols($this->Instruction);
- break;
- case "Oracle":
- $this->$result = ora_exec($Sql);
- break;
- case "ODBC":
- $this->$result = odbc_exec($Conn,$Sql);
- break;
- case "mysqli":
- $this->Query_ID = @mysqli_query($Conn,$Sql);
- $this->Error = @mysqli_error($Conn);
- if($this->Error) {
- $this->RecordCount = 0;
- $this->NumField = 0;
- } elseif (($this->Query_ID)&&(strpos(" ".strtoupper($Sql),"SELECT")) ) {
- $this->RecordCount = mysqli_num_rows($this->Query_ID);
- $this->NumField = mysqli_num_fields($this->Query_ID);
- } else {
- $this->RecordCount = 0;
- $this->NumField = 0;
- }
- return $this->Query_ID;
- break;
- default:
- $this->Query_ID = @mysql_query($Sql);
- $this->Error = @mysql_error();
- if($this->Error) {
- $this->RecordCount = 0;
- $this->NumField = 0;
- }
- elseif (($this->Query_ID)&&((strpos(" ".strtoupper(substr(trim($Sql),0,10)),"SELECT")) || (strpos(" ".strtoupper(substr(trim($Sql),0,4)),"SHOW"))) ) {
- $this->RecordCount = mysql_num_rows($this->Query_ID);
- $this->NumField = mysql_num_fields($this->Query_ID);
- } else {
- $this->RecordCount = 0;
- $this->NumField = 0;
- }
- return $this->Query_ID;
- break;
- }
- return $this->$result;
- }
-
- function NotEOF() {
- global $DBType;
- switch ($DBType) {
- case "OCI":
- $this->Line++;
- return ($this->Line<=$this->RecordCount);
- break;
- case "Oracle":
- $this->Record = ora_fetch($this->$Record);
- break;
- case "ODBC":
- $this->Record = odbc_fetch_into($this->$Record);
- break;
- case "mysqli":
- if($this->Error) return false;
- if($this->Query_ID) $this->Record = mysqli_fetch_array($this->Query_ID,MYSQLI_BOTH);
- break;
- default:
- if($this->Error) return false;
- if($this->Query_ID) $this->Record = mysql_fetch_array($this->Query_ID);
- break;
- }
- return $this->Record;
- }
-
- function Error() {
- global $Conn,$DBType;
- switch ($DBType) {
- case "OCI":
- $error = OCIError($Conn);
- return $error["message"];
- break;
- case "Oracle":
- return ora_error($this->$result);
- break;
- case "ODBC":
- return odbc_errormsg($Conn);
- break;
- default:
- return $this->Error;
- break;
- }
- }
-
- function f($Name) {
- global $DBType;
- switch ($DBType) {
- case "OCI":
- if(is_int($Name)) {
- $col = each($this->result);
- reset($this->result);
- for($I=0;$I<=$Name;$I++) {
- $col = each($this->result);
- $data = $col["value"];
- }
- return $data[$this->Line-1];
- } else {
- return $this->result[strtoupper($Name)][$this->Line-1];
- }
- break;
- default:
- if (isset($this->Record[$Name])) return $this->Record[$Name];
- break;
- }
- }
-
- function Reset() {
- reset($this->Record);
- }
-
- function TableName($No) {
- global $DBType;
- switch ($DBType) {
- case "OCI":break;
- case "Oracle":break;
- case "ODBC":break;
- default:return mysql_field_table($this->Query_ID,$No);break;
- }
- }
-
- function FieldName($No) {
- global $DBType;
- switch ($DBType) {
- case "OCI":return ociColumnName($this->Instruction,$No+1);break;
- case "Oracle":return ora_ColumnName($this->$result,$No);break;
- case "ODBC":return odbc_field_name($this->$result,$No);break;
- default:return @mysql_field_name($this->Query_ID,$No);break;
- }
- }
-
- function FieldType($No) {
- global $DBType;
- switch ($DBType) {
- case "OCI":return ociColumnType($this->Instruction,$No+1);break;
- case "Oracle":return ora_ColumnType($this->$result,$No);break;
- case "ODBC":return odbc_field_type($this->$result,$No);break;
- default:
- $Result = @mysql_field_type($this->Query_ID,$No);
- switch ($Result) {
- case "string":$Result = "VARCHAR";break;
- }
- return $Result;
- break;
- }
- }
-
- function FieldSize($No) {
- global $DBType;
- switch ($DBType) {
- case "OCI":return ociColumnSize($this->Instruction,$No+1);break;
- case "Oracle":return ora_ColumnSize($this->$result,$No);break;
- case "ODBC":return odbc_field_len($this->$result,$No);break;
- default:return @mysql_field_len($this->Query_ID,$No);break;
- }
- }
-
- function MoveNext() {
- // Fonction bidon
- }
-
- function CommitTrans() {
- // Fonction bidon
- }
-
- function RollBackTrans() {
- // Fonction bidon
- }
-
- function Close() {
- global $DBType;
- switch ($DBType) {
- case "OCI":
- if ( $this->$result != null ) return ociFreeStatement($this->Instruction);
- break;
- case "Oracle":
- if ( $this->$result != null ) return ora_close($this->$result);
- break;
- case "ODBC":
- if ( $this->$result != null ) return odbc_free_result($this->$result);
- break;
- case "mysqli":
- if($this->Query_ID ) {
- if($this->RecordCount>0) return mysqli_free_result($this->Query_ID);
- $this->RecordCount = 0;
- }
- break;
- default:
- if ( $this->Query_ID ) {
- if($this->RecordCount>0) return mysql_free_result($this->Query_ID);
- $this->RecordCount = 0;
- }
- break;
- }
- }
- }
Cette solution n'est bien sûre qu'une solution de compatibilité dans une 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.