pense-bête de bruno sanchiz

Accueil > Programmation > PHP > php:mysql

php:mysql

Publié le 19 mars 2017, dernière mise-à-jour le 27 février 2020, 12 visites, visites totales.

Entrée de données en php mysql : toute la page est obsolète.

entrée des données de la base

    $mysql_server="sql.free.fr" ( ou localhost en local)
    $mysql_nom="bruno.photos"; ( ou root en local )
    $mysql_base="bruno_photos": (ce qu on veut en local )
    $mysql_motdepasse="***********";

    $db=mysql_connect($mysql_server,$mysql_nom,$mysql_motdepasse); // ouverture de la base
    mysql_close($db); // fermeture de la base

exemple de l’affichage d’une base de donnée

       mysql_select_db($mysql_base); //sélection de la base
       $mysql_table="index"; //choix de la table
        $temp="SELECT * FROM <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+JnF1b3Q7LiRteXNxbF90YWJsZS4mcXVvdDs8L2NvZGU+"></span>" ;  //table
        $req=mysql_query($temp); //envoie de la requête temp

        $row=mysql_fetch_row($req); //réception de la première ligne de réponse

while ($row!=false){ //tant que la requête envoie une ligne
       echo "<br>";
        for ($i=0;$i<count($row);$i++) {echo " | ".$row[$i];} //affichage des valeurs des différents champs de chaque ligne
       $row=mysql_fetch_row($req); //réception de la ligne suivante
       }

tous les exemples

on a une table identite : numero | nom | prenom //eviter les accents

creation de la table

  $temp="CREATE TABLE IF NOT EXISTS <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span> ( <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bnVtZXJvPC9jb2RlPg=="></span> INT( 5 ) NOT NULL AUTO_INCREMENT, <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bm9tPC9jb2RlPg=="></span> VARCHAR( 30 ) NOT NULL , <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+cHJlbm9tPC9jb2RlPg=="></span> VARCHAR( 25 ) NOT NULL , INDEX ( <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bnVtZXJvPC9jb2RlPg=="></span> ) ) ENGINE = MYISAM " ;
        $req=mysql_query($temp) ;

insertion :

  $temp="INSERT INTO <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span> ( <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bnVtZXJvPC9jb2RlPg=="></span> , <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bm9tPC9jb2RlPg=="></span> , <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+cHJlbm9tPC9jb2RlPg=="></span> ) VALUES ( NULL , ’monnom’ ,’Monprenom' )" ;
        $req=mysql_query($temp) ;

changement des donnees

        $temp="UPDATE <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span> SET <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bm9tPC9jb2RlPg=="></span> = ’nouveau_nom’ WHERE <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bnVtZXJvPC9jb2RlPg=="></span> =1 AND CONVERT ( <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span>.<span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bm9tPC9jb2RlPg=="></span> USING utf8 ) = ’monnom’ AND CONVERT( <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span>.<span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+cHJlbm9tPC9jb2RlPg=="></span> USING utf8 ) = ’Monprenom’ LIMIT 1 ;
        $row=mysql_fetch_row($req) ;

recherche de l’existence d’une donnée

  $temp="SELECT * FROM <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span> WHERE <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+bm9tPC9jb2RlPg=="></span> LIKE ’monnom’ AND <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+cHJlbm9t4oCZIExJS0Ug4oCZTW9ucHJlbm9t4oCZICZxdW90OyA7DQogJm5ic3A7ICZuYnNwOyAmbmJzcDsgJm5ic3A7JHJlcT1teXNxbF9xdWVyeSgkdGVtcCkgOw0KICZuYnNwOyAmbmJzcDsgJm5ic3A7ICZuYnNwOyRyb3c9bXlzcWxfZmV0Y2hfcm93KCRyZXEpIDsNCiAmbmJzcDsgJm5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vZGVfaW5saW5lJyBkaXI9J2x0cic+JnF1b3Q7IDsNCiAmbmJzcDsgJm5ic3A7ICZuYnNwOyAmbmJzcDskcmVxPW15c3FsX3F1ZXJ5KCR0ZW1wKSA7DQombHQ7L2NvZGUmZ3Q7DQpMRVMgUFJPQkxFTUVTDQoNCiAmbmJzcDsgJm5ic3A7Z3JhbW1haXJlDQogJm5ic3A7ICZuYnNwOyAmbmJzcDsgJm5ic3A7Y2hhcXVlIGd1aWxsZW1ldCB2YSBwYXIgZGV1eA0KICZuYnNwOyAmbmJzcDsgJm5ic3A7ICZuYnNwO25lIHBhcyBzZSB0cm9tcGVyIGRhbnMgbGVzIHR5cGVzIGRlIGd1aWxsZW1ldHMgOg0KICZuYnNwOyAmbmJzcDsgJm5ic3A7ICZuYnNwOyAmbmJzcDsgJm5ic3A74oCZICh0b3VjaGUgNCApIGF1dG91ciBkZXMgZG9ubsOpZXMgKCDigJltb25ub23igJkgKTwvY29kZT4="></span> ( touche ALTGR 7 ) autour des champs et tables ( <span class="base64" title="PGNvZGUgY2xhc3M9J3NwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lJyBkaXI9J2x0cic+aWRlbnRpdGU8L2NvZGU+"></span> )
            " ( touche 3 ) pour les champs temp

    redondance L’utilisation de plusieurs requetes impose d’utiliser des variables $temp mais surtout $req et $row différentes

    oublis
        déclaration de la base
        changement de base pour chaque table : préciser <code>mysql_select_db($mysql_base)

 ; avant chaque requête


copie de fr.php.net

http://fr.php.net/manual/fr/functio...

[bruno sanchiz]