Code php générant une classe pour chaque table existante dans la base de donnée mysql

Contenu du snippet

C'est une classe PHP qui créé automatiquement une classe pour chaque table MySQL.

C'est une version bêta que j'ai développé à l'arrache...

Si vous trouvez des bugs n'hésitez pas à me signaler.

Amusez-vous bien......

à bientôt......

@+

Source / Exemple :


<?
class MysqlConnect {
	var $host; // Server Name
	var $user; // User Name
	var $password; // Password
	var $database; // DataBase Name
	var $idConnexion; // Connexion id
	var $isConnected; // True or False
	var $listdbArray; // Array
	
	var $table; // Default Table
	
	var $query;
	var $result;
	/********************************
	/  Constructor

                                                                  • /
function MysqlConnect() { $this->host = ""; $this->user = ""; $this->password = ""; $this->database = ""; $this->idConnexion = FALSE; $this->isConnected = FALSE; $this->listdbArray = array(); } /******************************** / Connexion to MySQL Server
                                                                  • /
function doConnect () { if ($this->isConnected == FALSE) { $this->idConnexion = @mysql_connect($this->getHost(), $this->getUser (), $this->getPassword()); if (!$this->idConnexion) { $this->MysqlConnexionErr(); $this->isConnected = FALSE; return 0; } $selectDb = @mysql_select_db($this->getDatabase($this->idConnexion)); if (!$selectDb) { $this->MysqlErr ('Unable to select this database : <b>'.$this->getDatabase().'</b>'); $this->isConnected = FALSE; $this->closeDb (); return 0; } } $this->isConnected = TRUE; //return $this->idConnexion; } /******************************** / MySQL DataBase Lists
                                                                  • /
function MysqlListsDb () { if ($this->isConnected == TRUE) { $listsdb = @mysql_list_dbs($this->idConnexion); $dbNumRows = @mysql_num_rows($listsdb); if ($dbNumRows > 0) { //$this->printMsg ($dbNumRows); $i=0; while($i<$dbNumRows) { $this->listdbArray [$i] = @mysql_tablename ($listsdb, $i); print '<a href="'.$_SERVER["PHP_SELF"].'?db='.$this->listdbArray [$i].'">'.$this->listdbArray [$i].'</a><br>'; $i++; } } else { $this->printMsg ('No database ...'); } @mysql_free_result ($listsdb); } } /******************************** / MySQL DataBase Table Lists
                                                                  • /
function MysqlListsTable ($strDb='') { //var_dump(isset($_REQUEST['db'])); if (isset($_REQUEST['db'])) { $listTable = mysql_list_tables($this->getDatabase(), $this->idConnexion); $tableNumRows = mysql_num_rows($listTable); if ($tableNumRows>0) { print '<hr>'; print 'Nombre de table disponible dans la base <b>'.$this->getDatabase().' : '.$tableNumRows.'</b> : <b style="color:blue;">'.$row.'</b><br>'; print 'Tables disponibles :<br>'; $i=0; while($i < $tableNumRows) { # # # Call Create Class Method to generate a class for each table in data base # # # //$this->create_class (mysql_tablename($listTable, $i)); print '<ul><li><a href="'.$_SERVER["PHP_SELF"].'?db='.$_REQUEST['db'].'&table='.mysql_tablename($listTable, $i).'">'.mysql_tablename($listTable, $i).'</a></li></ul>'; $i++; } } else { $this->printMsg ('Aucune table n\'est disponible dans cette base de donnée...'); } @mysql_free_result ($listTable); } } /******************************** / MySQL DataBase Table Fields
                                                                  • /
function MysqlListsFields () { if (!$this->table == '') { $qry = $this->MysqlSelect ($this->getTable()); $rs = $this->MysqlQuery ($qry); $row = @mysql_num_fields($rs); $nbCol = 3; $resteRowCol = $row % $nbCol; $nbLigne = ($row - $reste) / $nbCol; print $resteRowCol. ' => '.$nbLigne; if ($row > 0) { print '<hr>'; print 'Nombre de champ disponible dans la table <b>'.$this->getTable().'</b> : <b style="color:blue;">'.$row.'</b><br>'; print 'Champs disponibles :<br>'; $i=0; $j=1; print '<table border=0>'; while($obj = mysql_fetch_field ($rs)) { print ' <tr>'; print ' <td>'.$obj->name.'</td>'; if (($i+$j) == 3) { print ' </tr>'; } else { print ' </tr>'; //print '<ul><li> '.$j.' name : '.$obj->name.' => type : ' $obj->type.' => length : '. mysql_field_len ($rs, $i).'</li></ul>'; $i++; $j++; } } print '</table>'; print '<hr>'; } else { $this->printMsg ('Aucun champ n\'est disponible dans cette table...'); } @mysql_free_result ($rs); } } /********************************************************************************
  • Cette fonction crée automatiquement une class php
  • Donnez un nom de table du base donné MySQL
  • et le tour est joué....
  • Amusez vous bien :-)
                                                                                                                                                                  • /
# Method Create Class function create_class ($table_name='') { if (!empty($table_name)) { $out = ''; $out .= ''; $out = '&#60&#63'; // < ? $out .= "<br>"; $out .= "#File Name : cls_".$table_name.".php"; $out .= "<br>"; $out .= "#Date : ".date('Y-m-d - H:m:s'); $out .= "<br>"; $out .= "#Author : Coumarane COUPPANE"; $out .= "<br>"; $out .= "<br>"; $out .= 'class '.$table_name.' {'; $out .= "<br>"; $query = $this->MysqlSelect ($table_name); $result = $this->MysqlQuery ($query); $num_rows = mysql_num_fields ($result); $i=0; if ($num_rows > $i) { $num_fields = mysql_num_fields ($result); # Variables for ($k=0; $k<$num_fields; $k++) { $k_fields = mysql_fetch_field ($result, $k); if ($k_fields->primary_key == 1) { $idtab = $k_fields->name; } $out .= 'var $'.$k_fields->name.';'; $out .= "<br>"; } # Construct $out .= "<br>"; $out .= "# Construct"; $out .= "<br>"; $out .= 'function '.$table_name. ' () {'; $out .= "<br>"; for ($k=0; $k<$num_fields; $k++) { $k_fields = mysql_fetch_field ($result, $k); if ($k_fields->primary_key == 1) { $idtab = $k_fields->name; } $out .= '$this->'.$k_fields->name.' = "";'; $out .= "<br>"; } $out .= '}'; $out .= "<br>"; # Property SET $out .= "<br>"; $out .= '# Property SET'; $out .= "<br>"; for ($j=0; $j<$num_fields; $j++) { $fields = mysql_fetch_field ($result, $j); $out .= "<br>"; $out .= 'function set_'.$fields->name. ' ($'.$fields->name.') {'; $out .= "<br>"; $out .= '$this->'.$fields->name.' = $'.$fields->name.';'; $out .= "<br>"; $out .= '}'; $out .= "<br>"; } # Property GET $out .= "<br>"; $out .= '# Property GET'; $out .= "<br>"; for ($l=0; $l<$num_fields; $l++) { $l_fields = mysql_fetch_field ($result, $l); $out .= "<br>"; $out .= 'function get_'.$l_fields->name. ' () {'; $out .= "<br>"; $out .= 'return $this->'.$l_fields->name.';'; $out .= "<br>"; $out .= '}'; $out .= "<br>"; } #Function Insert Start ################################## $out .= '<br>'; $out .= '<br>'; $out .= '# Function Insert'; $out .= '<br>'; $out .= 'function '.$table_name.'_insert &#40&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "INSERT INTO '.$table_name.' (<br>'; $z = 1; for ($j=0; $j<$num_fields; $j++) { $fields = mysql_fetch_field ($result, $j); if ($z == $num_fields) { $virgule = ''; } else {$virgule = ',';} $out .= $fields->name.$virgule.'<br>'; //$out .= 'get_'.$fields->name; $z++; } $out .= ') VALUES (<br>'; $w = 1; for ($j=0; $j<$num_fields; $j++) { $fields = mysql_fetch_field ($result, $j); if ($w == $num_fields) { $virgule = ''; } else {$virgule = ',';} $out .= '\'".$this->get_'.$fields->name.' ()."\''.$virgule.'<br>'; $w++; } $out .= ')";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; #Function Insert End ################################ #Function Select Start ################################# $out .= '<br>'; $out .= '<br>'; $out .= '# Function Select'; $out .= '<br>'; $out .= 'function '.$table_name.'_select &#40&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "SELECT * FROM '.$table_name.'";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; #Function Select End ################################# #Function Fields Select Start ################################# $out .= '<br>'; $out .= '<br>'; $out .= '# Function Fields Select'; $out .= '<br>'; $out .= 'function '.$table_name.'_fields_select &#40$fieldName, $data&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "SELECT * FROM '.$table_name.' WHERE ".$fieldName." = \'".$data."\'";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; #Function Fields Select End ################################# #Function Update Start ############################### $out .= '<br>'; $out .= '<br>'; $out .= '# Function Update'; $out .= '<br>'; $out .= 'function '.$table_name.'_update &#40&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "UPDATE '.$table_name.' SET<br>'; $y = 1; for ($j=0; $j<$num_fields; $j++) { $fields = mysql_fetch_field ($result, $j); if ($y == $num_fields) { $virgule = ''; } else {$virgule = ',';} $out .= $fields->name.' = \'".$this->get_'.$fields->name.'( )."\''.$virgule.'<br>'; $y++; } $out .= '";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; #Function Update End ################################## #Function Fields Update Start ############################# $out .= '<br>'; $out .= '<br>'; $out .= '# Function Fields Update'; $out .= '<br>'; $out .= 'function '.$table_name.'_fields_update &#40$fieldName, $data&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "UPDATE '.$table_name.' SET ".$fieldName." = \'".$data."\'";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; #Function Fields Update End ############################## #Function Delete Start ################################# $out .= '<br>'; $out .= '<br>'; $out .= '# Function Delete'; $out .= '<br>'; $out .= 'function '.$table_name.'_delete &#40&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "DELETE FROM '.$table_name.'";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; #Function Delete End ################################# #Function Fields Delete Start ################################# $out .= '<br>'; $out .= '<br>'; $out .= '# Function Fields Delete'; $out .= '<br>'; $out .= 'function '.$table_name.'_fields_delete &#40$fieldName, $data&#41 &#123'; // &#123:{ &#40:( &#41:) $out .= '<br>'; $out .= '$query = "DELETE FROM '.$table_name.' WHERE ".$fieldName." = \'".$data."\'";'; $out .= '<br>'; $out .= '$result = mysql_query ($query);'; $out .= '<br>'; $out .= 'if (!$result) { return false;}'; $out .= '<br>'; $out .= 'return $result;'; $out .= '<br>'; $out .= '}'; $out .= '<br>'; #Function Fields Delete End ################################# #Function Table Nb Records Start ############################## $out .= '<br>'; $out .= '# Function Table Nb Records'; $out .= '<br>'; $out .= 'function '.$table_name.'_num_rows () {'; $out .= '<br>'; $out .= ' $result = $this->'.$table_name.'_select ();'; $out .= '<br>'; $out .= ' if (!$result) {'; $out .= '<br>'; $out .= ' $this->err_mysql () ;'; $out .= '<br>'; $out .= ' return false;'; $out .= '<br>'; $out .= ' }'; $out .= '<br>'; $out .= ' if (($rows = mysql_num_rows ($result)) < 0) {'; $out .= '<br>'; $out .= ' return false;'; $out .= '<br>'; $out .= ' }'; $out .= '<br>'; $out .= ' return $rows;'; $out .= '<br>'; $out .= '}'; $out .= '<br>'; #Function Table Nb Records End ############################### # Function Table Loop Start ################################# $out .= '<br>'; $out .= '# Function Table Loop'; $out .= '<br>'; $out .= 'function '.$table_name.'_loop () {'; $out .= '<br>'; $out .= '$result = $this->'.$table_name.'_select ();'; $out .= '<br>'; $out .= 'if (!$result) {'; $out .= '<br>'; $out .= '$this->err_mysql () ;'; $out .= '<br>'; $out .= 'return false;'; $out .= '<br>'; $out .= '}'; $out .= '<br>'; $out .= '$rows = $this->'.$table_name.'_num_rows ();'; $out .= '<br>'; $out .= 'if ($rows > 0) {'; $out .= '<br>'; $out .= 'while ($obj = mysql_fetch_assoc ($result)) {'; $out .= '<br>'; $out .= '$tab_content [] = $obj;'; $out .= '<br>'; $out .= '}'; $out .= '<br>'; $out .= 'return $tab_content; '; $out .= '<br>'; $out .= '}'; $out .= '<br>'; $out .= '}'; $out .= '<br>'; # Function Table Loop End ################################## #Function Err MySQL Start ################################# $out .= '<br>'; $out .= '# Function Err MySQL'; $out .= '<br>'; $out .= 'function err_mysql () {'; $out .= '<br>'; $out .= ' print "Error : ".mysql_error(). " File : ".__FILE__;'; $out .= '<br>'; $out .= '}'; #Function Err MySQL End ################################# $out .= '<br>'; $out .= '&#125'; // &#125:} $out .= '<br>'; $out .= '&#63&#62'; // &#63:? // &#62:> $out .= '<br>'; print $out; } else { print 'Nothing to display...'; } mysql_free_result ($result); } } /******************************** / MySql Select
                                                                  • /
function MysqlSelect ($tablename, $idname='', $id='') { if ($idname != '' AND $id != '') { $this->query = 'SELECT * FROM '.$tablename.' WHERE '.$idname.' = "'.$id.'"'; } else { $this->query = 'SELECT * FROM '.$tablename; } return ($this->query); } /******************************** / MySql Query
                                                                  • /
function MysqlQuery () { $this->result = @mysql_query ($this->query); if (!$this->result) { $this->MysqlErr('Vérifiez la requête : '.$this->query.'<br>'); return 0; } return ($this->result); } /******************************** / Print message
                                                                  • /
function printMsg ($strMsg) { print '<font style="font-family:arial, helvetica; font-size:12px; color:blue;">'; print $strMsg.'<hr>'; print '</font>'; } /******************************** / Close MySQL Server Connexion
                                                                  • /
function closeDb () { if ($this->isConnected == TRUE) { @mysql_close($this->idConnexion); } $this->isConnected = FALSE; } /******************************** / MySQL Server Connexion Erreur
                                                                  • /
function MysqlConnexionErr () { print '<font style="font-family:arial, helvetica; font-size:12px; color:red">'; print 'Unable to connect to the SERVER : <b>'.$this->getHost().'</b><br>'; print '</font>'; } /******************************** / MySQL Server Erreur
                                                                  • /
function MysqlErr ($msg) { print '<font style="font-family:arial, helvetica; font-size:12px; color:red">'; print $msg.'<hr>'; print 'Erreur SQL : '.@mysql_error($this->idConnexion).'<br>'; print 'Erreur no SQL : '.@mysql_errno($this->idConnexion).'<br>'; print '</font>'; } /******************************** / Property SET
                                                                  • /
function setHost ($strHost='') { if (empty($strHost)) { $this->host = 'localhost'; } else { $this->host = $strHost; } } function setUser ($strUser='') { $this->user = $strUser; } function setPassword ($strPass='') { if (empty($strPass)) { $this->password = ''; } else { $this->password = $strPass; } } function setDatabase ($strDbase='') { if (empty($strDbase)) { $this->database = 'mysql'; } else { $this->database = $strDbase; } } function setTable ($table='') { if (isset($_REQUEST['table'])) { $this->table = $table; } else { $this->table = ''; } /* if (empty($table)) { $this->table = 'user'; } else { $this->table = $table; }
  • /
} /******************************** / Property GET
                                                                  • /
function getHost () { return $this->host; } function getUser () { return $this->user; } function getPassword () { return $this->password; } function getDatabase () { return $this->database; } function getTable () { return $this->table; } } ?>

Conclusion :


// Pour le tester ::
// Copiez et coller les lignes ci-dessous dans la même page que le code ci dessus.

<?
// Example

$cnx = new MysqlConnect (); // Create New Class $cnx
$cnx->setHost(); // Assign Host Name
$cnx->setUser(); // Assign User Name
$cnx->setPassword(''); // Assign Password
$cnx->setDatabase($_GET['db']); // Assign DataBase Name
$cnx->setTable($_GET['table']); // Assign Table Name
$cnx->doConnect(); // Connexion...
$cnx->MysqlListsDb (); // Output DataBase Lists
$cnx->MysqlListsTable(); // Output Table Lists
$cnx->MysqlListsFields ();
$cnx->create_class ($_GET['table']);
$cnx->closeDb(); // Close opened connexion

?>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.