Vous êtes ici Codes PHP et MySQL > Commandes > Fonction PHP équivalente à la commande DESCRIBE de...

 

Fonction PHP équivalente à la commande DESCRIBE de MySQL

Cette retourne un tableau HTML contenant les données renvoyées par la commande DESCRIBE concernant la table passée en paramètre.

Ce paramètre obligatoire est de type String : le nom de la table.

La page d'éxécution doit bien sûr être déja connectée à MySQL et une base sélectionnée.

Le code généré est indenté, valide et sémantique.

La fonction n'intègre pas la gestion des erreurs (connexion au serveur, à la base, existence de la table...).

Voici le CSS que je vous conseille d'utiliser pour ces tableaux d'affichage de données "brutes" de MySQL/PHP :
table {
border-collapse: collapse;
}
table, tr, th, td {
border: 1px solid black;
}
th, td {
padding: 1ex;
}
th {
font-weight: bold;
text-align: center;
}
 
poujolrost-mathias
Site de l'auteur voir
[2] sources en MySQL voir
Code vu 2663 fois
Enregistré le 23 Avril 2009
  • Digg ce code sur digg.com
  • Bookmark ce code sur del.icio.us
  • Bookmark ce code sur Google
  • Bookmark ce code sur Yahoo
  • Ajoute Fonction PHP équivalente à la commande DESCRIBE de MySQL
  • Partage ce code sur Facebook
 
 
 
 

Code Source


01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
 
<?php
/******************************************************************************/
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : Fonction PHP équivalente à la commande DESCRIBE de...     */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts306-MySQL.htm            */
/* Auteur         : poujolrost-mathias                                        */
/* Date édition   : 23 Avril 2009                                             */
/* Website auteur : http://site.mathiaspoujolrost.net/competences/            */
/*                                                                            */
/******************************************************************************/

/* renvoie le tableau de la commande "DESCRIBE" de MySQL */
function mysql_describe_table($table)
{
    $retour ""// typage en string
    $styleTable " style=\"border-collapse: collapse; border: 1px solid black;\"";
    $styleTh " style=\"border: 1px solid black; padding: 1ex;\"";
    $styleTd " style=\"border: 1px solid black; padding: 1ex;\"";
    /* je vous conseille plutôt une feuille CSS externe ! */
    
    $req "DESCRIBE "mysql_real_escape_string($table);
    $resultat mysql_query($req);
    
    $retour .= "\n
<!-- DEBUT structure de la table $table (fonction String" .
" describe_table(String \$table)) -->
    <table$styleTable>
    <caption>Structure de la table <samp>$table</samp> (<a
    href=\"http://dev.mysql.com/doc/refman/5.0/fr/describe.html\" title=\"lien
    externe vers la documentation officielle\">aide
    sur la commande &quot;DESCRIBE&quot; de MySQL</a>)</caption>\n";
    $retour .= "\t<tr>
    \t\t<th scope=\"col\"$styleTh>Nom de <br />la colonne</th>
    \t\t<th scope=\"col\"$styleTh>Type</th>
    \t\t<th scope=\"col\"$styleTh>A le droit<br /> d'&ecirc;tre vide</th>
    \t\t<th scope=\"col\"$styleTh>Indexation<br /> (clef)</th>
    \t\t<th scope=\"col\"$styleTh>Valeur <br />par d&eacute;faut</th>
    \t\t<th scope=\"col\"$styleTh>Informations <br />suppl&eacute;mentaires</th>
    \t</tr>\n";
    while ($ligne mysql_fetch_assoc($resultat))
    {
        $retour .= "\t<tr>\n";
        foreach ($ligne as $valeur)
        {
            $retour .= "\t\t<td$styleTd>$valeur</td>\n";
        }
        $retour .= "\t</tr>\n";
    }
    $retour .= "</table>
<!-- FIN structure de la table $table (fonction String" .
" mysql_describe_table(String \$table)) -->\n";
    
    return $retour;    
}


/* exemple d'appel */
mysql_connect("host","username","password");
mysql_select_db("dotclear2dot1dot5");
echo mysql_describe_table("dc_media");


/* exemple de rendu HTML */
?>
<!-- DEBUT structure de la table dc_media (fonction String describe_table(String
 $table)) -->
    <table style="border-collapse: collapse; border: 1px solid black; padding:
 1em;">
    <caption>Structure de la table <samp>dc_media</samp> (<a
    href="http://dev.mysql.com/doc/refman/5.0/fr/describe.html" title="lien
    externe vers la documentation officielle">aide
    sur la commande &quot;DESCRIBE&quot; de MySQL</a>)</caption>
    <tr>
            <th scope="col" style="border: 1px solid black; padding: 1ex;">Nom de <br
 />la colonne</th>
            <th scope="col" style="border: 1px solid black; padding: 1ex;">Type</th>
    
        <th scope="col" style="border: 1px solid black; padding: 1ex;">A le droit<br
 /> d'&ecirc;tre vide</th>
            <th scope="col" style="border: 1px solid black; padding:
 1ex;">Indexation<br /> (clef)</th>
            <th scope="col" style="border: 1px solid black; padding: 1ex;">Valeur <br
 />par d&eacute;faut</th>
            <th scope="col" style="border: 1px solid black; padding:
 1ex;">Informations <br />suppl&eacute;mentaires</th>
        </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_id</td>
        <td style="border: 1px solid black; padding: 1ex;">bigint(20)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;">PRI</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">user_id</td>
        <td style="border: 1px solid black; padding: 1ex;">varchar(32)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;">MUL</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_path</td>
        <td style="border: 1px solid black; padding: 1ex;">varchar(255)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_title</td>
        <td style="border: 1px solid black; padding: 1ex;">varchar(255)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_file</td>
        <td style="border: 1px solid black; padding: 1ex;">varchar(255)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_dir</td>
        <td style="border: 1px solid black; padding: 1ex;">varchar(255)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;">.</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_meta</td>
        <td style="border: 1px solid black; padding: 1ex;">longtext</td>
        <td style="border: 1px solid black; padding: 1ex;">YES</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_dt</td>
        <td style="border: 1px solid black; padding: 1ex;">datetime</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;">1970-01-01 00:00:00</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_creadt</td>
        <td style="border: 1px solid black; padding: 1ex;">datetime</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;">1970-01-01 00:00:00</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_upddt</td>
        <td style="border: 1px solid black; padding: 1ex;">datetime</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;">1970-01-01 00:00:00</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
    <tr>
        <td style="border: 1px solid black; padding: 1ex;">media_private</td>
        <td style="border: 1px solid black; padding: 1ex;">smallint(6)</td>
        <td style="border: 1px solid black; padding: 1ex;">NO</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
        <td style="border: 1px solid black; padding: 1ex;">0</td>
        <td style="border: 1px solid black; padding: 1ex;"></td>
    </tr>
</table>
<!-- FIN structure de la table dc_media (fonction String describe_table(String
 $table)) -->
 

Fonctions du code

: Protège les caractères spéciaux d'une commande SQL - (PHP 4 >= 4.3.0, PHP 5,...
: Envoie une requête à un serveur MySQL - (PHP 4, PHP 5, PECL mysql:1.0)
: Lit une ligne de résultat MySQL dans un tableau associatif - (PHP 4 >= 4.0.3, PHP...
: Ouvre une connexion à un serveur MySQL - (PHP 4, PHP 5, PECL mysql:1.0)
: Sélectionne une base de données MySQL - (PHP 4, PHP 5, PECL mysql:1.0)
: Affiche une chaîne de caractères - (PHP 4, PHP 5)
Ajouter un commentaire
Code de sécurité

Attention: Les champs marqués d'une étoile * sont obligatoires
 
Librairie PHP

Connexion

 
 

Commandes

 
 

MySQL

 
 
 
 
    Offres d'emploi

Plus de 500 offres d'emploi PHP/MySQL

Offres d'emploi
 
    Editeur PHP
 
        Publicité