{"id":490,"date":"2023-09-27T09:07:08","date_gmt":"2023-09-27T13:07:08","guid":{"rendered":"https:\/\/ve2cuy.com\/?page_id=490"},"modified":"2024-09-18T10:07:16","modified_gmt":"2024-09-18T14:07:16","slug":"420-5d3-connexion-php-avec-oracle","status":"publish","type":"page","link":"https:\/\/ve2cuy.com\/index.php\/420-5d3-connexion-php-avec-oracle\/","title":{"rendered":"420-5D3 : Connexion php avec Oracle"},"content":{"rendered":"\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">EXEMPLE<\/mark><\/strong> &#8211; D&rsquo;une application php qui utilise le SGBD Oracle.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/mark><\/strong>: Exercices \u00e0 r\u00e9aliser par l&rsquo;\u00e9tudiant en classe.  Il faut programmer et adapter et ex\u00e9cuter les scripts suivants.  Adapter les scripts en fonction du sch\u00e9ma de votre base de donn\u00e9es. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1 &#8211; Lancer un conteneur php+apache+clientOracle<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Utilisation d'une image Docker php+Apache+ClientOracle\n# Pour PHP8+ORI+Argon2\ndocker pull paliari\/apache-php8-oci8:1.3.0-dev\n$ docker run -itd --name php-oci -v $(pwd)\/web:\/var\/www\/html\/public -p 80:80 paliari\/apache-php8-oci8:1.3.0-dev\n\n#---------------------------------------------------------------------------------------\n# Pour PHP7+ORI\n$ docker run -itd --name php-oci -v \/Users\/alain\/docker.projets\/web:\/var\/www\/html -p 80:80 adrianharabula\/php7-with-oci8\n\nC\\> docker run -itd --name php-oci -v C:\\Users\\Alain\\web:\/var\/www\/html -p 88:80 adrianharabula\/php7-with-oci8\n\n<\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/mark><\/strong>: Oracle propose des Dockerfiles pour b\u00e2tir des images en fonction des besoins des d\u00e9veloppeurs.  Ces fichiers sont <a href=\"https:\/\/github.com\/oracle\/docker-images\/tree\/main\/OracleLinuxDevelopers\">ici<\/a>. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2 &#8211; R\u00e9diger un script php de connexion \u00e0 la BD<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\/\/ Informations de connexion \u00e0 la base de donn\u00e9es Oracle\n\/\/ Informations de connexion \u00e0 la base de donn\u00e9es Oracle\n$db_username = 'etudiant';\n$db_password = 'password';\n$db_service = 'CSTJ4205D3';\n$db_host = '192.168.2.51';\n\n\/\/ Connexion \u00e0 la base de donn\u00e9es Oracle\n$conn = oci_connect($db_username, $db_password, $db_host . '\/' . $db_service);\n\nif (!$conn) {\n    die(\"\u00c9chec de la connexion \u00e0 la base de donn\u00e9es \ud83e\udd2a : \" . oci_error());\n} else {\n    print(\"Connexion au SGBD Oracle r\u00e9ussi \ud83e\udd13 !\");\n}\n\n?><\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/mark><\/strong>: Ce script doit-\u00eatre dans le dossier racine Web du conteneur!  Oui, je sais, vous le saviez d\u00e9j\u00e0 \ud83e\udd2a.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Tester le script de connexion:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"http:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-09.42.56-1024x502.png\" alt=\"\" class=\"wp-image-494\" style=\"width:492px;height:241px\" srcset=\"https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-09.42.56-1024x502.png 1024w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-09.42.56-300x147.png 300w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-09.42.56-768x376.png 768w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-09.42.56.png 1372w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3 &#8211; Ex\u00e9cuter une requ\u00eate SQL Oracle simple avec php<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\/\/ Inclure le fichier de configuration de la base de donn\u00e9es\ninclude('config.php');\n\n\/\/ Requ\u00eate SQL pour r\u00e9cup\u00e9rer les albums de Tintin\n\/\/ Auteur = Herg\u00e9 = auteur_id = 1\n# $sql = \"SELECT titre FROM Albums\";\n\n$sql = \"SELECT titre FROM Albums WHERE auteur_id = (SELECT auteur_id FROM Auteurs WHERE nom LIKE 'Herg%')\";\n\n\/\/ Ex\u00e9cuter la requ\u00eate SQL\n$result = oci_parse($conn, $sql);\noci_execute($result);\n?>\n\n&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n    &lt;title>Albums de Tintin&lt;\/title>\n&lt;\/head>\n&lt;body>\n  &lt;hr\/>\n  &lt;h1>Albums de Tintin&lt;\/h1>\n    &lt;ul>\n        &lt;?php\n        \/\/ Afficher les albums dans une liste\n        while ($row = oci_fetch_assoc($result)) {\n            echo '&lt;li>' . htmlspecialchars($row['TITRE']) . '&lt;\/li>';\n        }\n        ?>\n    &lt;\/ul>\n&lt;\/body>\n&lt;\/html>\n\n&lt;?php\n\/\/ Fermer la connexion \u00e0 la base de donn\u00e9es\noci_close($conn);\n?>\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1017\" src=\"http:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-10.01.30-1024x1017.png\" alt=\"\" class=\"wp-image-495\" style=\"width:456px;height:453px\" srcset=\"https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-10.01.30-1024x1017.png 1024w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-10.01.30-300x298.png 300w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-10.01.30-150x150.png 150w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-10.01.30-768x762.png 768w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-10.01.30.png 1372w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4 &#8211; Afficher la liste des albums par personnage<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    &lt;title>Liste de tous les Albums&lt;\/title>\n    &lt;!-- Liens Bootstrap CSS -->\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.3.0\/dist\/css\/bootstrap.min.css\">\n    &lt;style>\n        \/* Style personnalis\u00e9 pour l'espacement *\/\n        .character {\n            margin-top: 20px;\n        }\n\n        \/* Style personnalis\u00e9 pour la liste \u00e0 puces *\/\n        .album-list {\n            padding-left: 20px;\n        }\n\n        \/* Style personnalis\u00e9 pour les albums impairs *\/\n        .album-list li:nth-child(odd) {\n            background-color: #f2f2f2; \/* Couleur d'arri\u00e8re-plan pour les albums impairs *\/\n        }\n\n        \/* Style personnalis\u00e9 pour les images des personnages *\/\n        .character-image {\n            max-width: 100px; \/* Largeur maximale de l'image *\/\n            height: auto; \/* Ajuster la hauteur proportionnellement *\/\n        }\n\n        \/* Style personnalis\u00e9 pour le carr\u00e9 gris *\/\n        .no-photo {\n            width: 100px; \/* Largeur du carr\u00e9 gris *\/\n            height: 100px; \/* Hauteur du carr\u00e9 gris *\/\n            background-color: #ccc; \/* Couleur de fond gris *\/\n        }\n    &lt;\/style>\n&lt;\/head>\n&lt;body>\n    &lt;div class=\"container mt-5\">\n        &lt;h1 class=\"text-center\">Liste de tous les Albums&lt;\/h1>\n        &lt;?php\n        \/\/ Inclure le fichier de configuration de la base de donn\u00e9es\n        include('config.php');\n\n        \/\/ Requ\u00eate SQL pour r\u00e9cup\u00e9rer tous les albums de tous les personnages\n        $sql = \"SELECT P.nom AS personnage, P.photo AS photo, A.titre AS album\n                FROM Personnages P\n                LEFT JOIN Albums_Personnages PA ON P.personnage_id = PA.personnage_id\n                LEFT JOIN Albums A ON PA.album_id = A.album_id\n                ORDER BY P.nom, A.titre\";\n\n        $result = oci_parse($conn, $sql);\n        oci_execute($result);\n\n        \/\/ Initialisation des variables pour suivre le personnage actuel\n        $currentPersonnage = null;\n\n        while ($row = oci_fetch_assoc($result)) {\n            $personnage = htmlspecialchars($row['PERSONNAGE']);\n            $album = htmlspecialchars($row['ALBUM']);\n            $photo = htmlspecialchars($row['PHOTO']); \/\/ Source de la photo du personnage\n\n            \/\/ Si le personnage actuel change, affichez le nouveau personnage\n            if ($currentPersonnage !== $personnage) {\n                \/\/ Fermez la liste pr\u00e9c\u00e9dente si ce n'est pas le premier personnage\n                if ($currentPersonnage !== null) {\n                    \/\/ Affichez le message si le personnage n'a pas d'albums\n                    if (!$hasAlbum) {\n                        echo '&lt;p>Ce personnage n\\'est pas dans les albums pr\u00e9sents dans la base de donn\u00e9es.&lt;\/p>';\n                    }\n                    echo '&lt;\/ul>';\n                }\n\n                \/\/ R\u00e9initialisez la variable $hasAlbum\n                $hasAlbum = false;\n\n                \/\/ Affichez le nom du nouveau personnage avec une classe Bootstrap\n                echo '&lt;div class=\"character\">';\n                echo '&lt;h2>' . $personnage . '&lt;\/h2>';\n\n                \/\/ Affichez la photo du personnage ou un carr\u00e9 gris s'il n'y a pas de photo\n                if (!empty($photo)) {\n                    echo '&lt;img class=\"character-image\" src=\"' . $photo . '\" alt=\"' . $personnage . '\">';\n                } else {\n                    echo '&lt;div class=\"no-photo\">&lt;\/div>';\n                }\n\n                echo '&lt;ul class=\"list-unstyled album-list\">';\n                $currentPersonnage = $personnage;\n            }\n\n            \/\/ Affichez l'album dans la liste \u00e0 puces avec une classe Bootstrap\n            echo '&lt;li>' . $album . '&lt;\/li>';\n        }\n\n        \/\/ Fermez la derni\u00e8re liste \u00e0 puce\n        if ($currentPersonnage !== null) {\n           echo '&lt;\/ul>';\n        }\n\n        \/\/ Fermer la connexion \u00e0 la base de donn\u00e9es\n        oci_close($conn);\n        ?>\n    &lt;\/div>\n\n    &lt;!-- Liens Bootstrap JavaScript -->\n    &lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.3.0\/dist\/js\/bootstrap.min.js\">&lt;\/script>\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"1006\" src=\"http:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-11.40.01.png\" alt=\"\" class=\"wp-image-497\" style=\"width:292px;height:374px\" srcset=\"https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-11.40.01.png 786w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-11.40.01-234x300.png 234w, https:\/\/ve2cuy.com\/wp-content\/uploads\/2023\/09\/Capture-decran-le-2023-09-27-a-11.40.01-768x983.png 768w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-red-color has-text-color\">D\u00e9fi suppl\u00e9mentaire<\/h2>\n\n\n\n<p>Utiliser symphony pour programmer un formulaire permettant l&rsquo;ajout d&rsquo;un personnage dans la BD.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/mark><\/strong>: Il faudra peut-\u00eatre installer symphony dans le conteneur ou proposer une autre solution.  Pas facile d&rsquo;\u00eatre en troisi\u00e8me ann\u00e9e &#8230; \ud83e\udd75<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">INDICE de r\u00e9alisation:<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ src\/Controller\/PersonnageController.php\n\nuse App\\Entity\\Personnage;\nuse App\\Form\\PersonnageType;\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nuse Symfony\\Component\\HttpFoundation\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\nuse Symfony\\Component\\Routing\\Annotation\\Route;\n\nclass PersonnageController extends AbstractController\n{\n    \/**\n     * @Route(\"\/ajouter-personnage\", name=\"ajouter_personnage\")\n     *\/\n    public function ajouterPersonnage(Request $request): Response\n    {\n        $personnage = new Personnage();\n        $form = $this->createForm(PersonnageType::class, $personnage);\n\n        $form->handleRequest($request);\n\n        if ($form->isSubmitted() &amp;&amp; $form->isValid()) {\n            \/\/ Sauvegardez le personnage en base de donn\u00e9es\n            $entityManager = $this->getDoctrine()->getManager();\n            $entityManager->persist($personnage);\n            $entityManager->flush();\n\n            return $this->redirectToRoute('accueil'); \/\/ Redirigez vers la page d'accueil ou une autre page\n        }\n\n        return $this->render('personnage\/ajouter.html.twig', [\n            'form' => $form->createView(),\n        ]);\n    }\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{# templates\/personnage\/ajouter.html.twig #}\n\n{% extends 'base.html.twig' %}\n\n{% block title %}Ajouter un Personnage{% endblock %}\n\n{% block body %}\n    &lt;h1>Ajouter un Personnage&lt;\/h1>\n\n    {{ form_start(form) }}\n    {{ form_widget(form) }}\n    &lt;button type=\"submit\" class=\"btn btn-primary\">Ajouter&lt;\/button>\n    {{ form_end(form) }}\n{% endblock %}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># config\/routes.yaml\najouter_personnage:\n    path: \/ajouter-personnage\n    controller: App\\Controller\\PersonnageController::ajouterPersonnage<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>EXEMPLE &#8211; D&rsquo;une application php qui utilise le SGBD Oracle. NOTE: Exercices \u00e0 r\u00e9aliser par l&rsquo;\u00e9tudiant en classe. Il faut programmer et adapter et ex\u00e9cuter les scripts suivants. Adapter les scripts en fonction du sch\u00e9ma de votre base de donn\u00e9es. 1 &#8211; Lancer un conteneur php+apache+clientOracle NOTE: Oracle propose des Dockerfiles pour b\u00e2tir des images [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-490","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/pages\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":16,"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/pages\/490\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/pages\/490\/revisions\/1026"}],"wp:attachment":[{"href":"https:\/\/ve2cuy.com\/index.php\/wp-json\/wp\/v2\/media?parent=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}