1 2 3 4 5 6 7 8 9 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
| <!doctype html>
<html>
<head>
<title>XML !</title>
<body>
<?php
/*----------------------------------------------*/
/* Récupération fichier au format 'XML' */
/*----------------------------------------------*/
$xml = simplexml_load_file('cli.xml');
$length = count($xml->cli);
/*-----------------------------------------------------*/
/* Informations pour la connexion à la base de données */
/*-----------------------------------------------------*/
$nom_du_serveur = "mysql:host=localhost;dbname=base";
$nom_utilisateur = "root";
$mot_de_passe = "root";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES latin1",
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_AUTOCOMMIT
);
/*----------------------*/
/* Connexion au Serveur */
/*----------------------*/
try {
$link = new PDO($nom_du_serveur, $nom_utilisateur, $mot_de_passe, $options);
}
catch (Exception $e)
{
echo "Connection à MySql impossible : ", $e->getMessage();
die();
}
/*---------------------*/
/* lecture fichier xml */
/*---------------------*/
$ins = $link->prepare("insert into client (`num_id`,`nom`,`prenom`,`relation`,`adresse_c`,`code_postal`,`ville`) values (:p1,:p2,:p3,:p4,:p5,:p6,:p7)");
$i=0;
while ($i < $length)
{
try {
$ins->bindParam('p1', $xml->cli[$i]->num_id);
$ins->bindParam('p2', $xml->cli[$i]->contact->nom);
$ins->bindParam('p3', $xml->cli[$i]->contact->prenom);
$ins->bindParam('p4', $xml->cli[$i]->relation);
$ins->bindParam('p5', $xml->cli[$i]->adresse->adresse_c);
$ins->bindParam('p6', $xml->cli[$i]->adresse->code_postal);
$ins->bindParam('p7', $xml->cli[$i]->adresse->ville);
$ins->execute();
}
catch ( Exception $e )
{
echo "Une erreur est survenue dans le l'insert : ", $e->getMessage();
die();
}
$i++;
}
/*---------------------------*/
/* Vidage de la table client */
/*---------------------------*/
try {
$sel = $link->query("SELECT * FROM client");
}
catch ( Exception $e )
{
echo "Une erreur est survenue dans le select : ", $e->getMessage();
die();
}
echo "<pre>";
printf("+--------+--------+--------+----------+-----------------+-------------+----------+\n");
printf("| Num Id | Nom | Prénom | Relation | Adresse | Code Postal | Ville |\n");
printf("+--------+--------+--------+----------+-----------------+-------------+----------+\n");
try {
while ($row = $sel->fetch(PDO::FETCH_ASSOC))
printf("|%7d |%7s |%7s |%9s |%16s |%12s |%9s |\n", $row['num_id'],$row['nom'],$row['prenom'],$row['relation'],$row['adresse_c'],$row['code_postal'],$row['ville']);
}
catch ( Exception $e )
{
echo "Une erreur est survenue dans la lecture du select : ", $e->getMessage();
die();
}
printf("+--------+--------+--------+----------+-----------------+-------------+----------+\n");
echo "</pre>";
$sel->closeCursor();
/*-----*/
/* fin */
/*-----*/
$link = null;
?>
</body>
</html> |
Partager