Meo's Web site
     

http://www.meo-x.net

     Home | Information | Know-how | Sections | Documents | Services | Plus |
Didier Meo: Excel VBA Kurs

 

 

 

E-MAIL:
mail to meo-x.net  Send a mail           

 

Eine einfache Probe für die Nutzung von PHP mit mysqli und PDO

 

Dieses Modul wurde von Didier Meo programmiert.

Un échantillon facile pour l'utilisation de PHP avec mysqli et PDO

 

Ce module a été programmé par Didier Meo.

An easy sample for the usage of PHP with mysqli and PDO

 

This module was programming by Didier Meo.
..............................................                





/*------------------------------------------------------------------------------- * FILE: index.php * Author: Didier MEO * Licence: free MEO Education * An easy sample for the usage of PHP with mysqli and PDO */ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="./meo1.css" type="text/css"> </head> <body> <?php if(isset($_POST['update']) || isset($_POST['delete'])) { $dsn = 'mysql:dbname=dbmeotest;host=127.0.0.1'; $user = 'root'; $password = ''; try { $dbh = new PDO($dsn, $user, $password); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo 'PDO connection failed: ' . $e->getMessage(); exit; } try { $id = $_POST['id']; $author = $_POST['author']; $title = $_POST['title']; $version = $_POST['version']; $link = $_POST['link']; $reference = $_POST['reference']; $dbh->beginTransaction(); if(isset($_POST['update'])) $dbh->exec("UPDATE dbmeotest.article SET author='$author', title='$title', version='$version', link='$link', reference='$reference' WHERE id='$id'"); if(isset($_POST['delete'])) $dbh->exec("DELETE FROM dbmeotest.article WHERE id='$id'"); $dbh->commit(); } catch (Exception $e) { $dbh->rollBack(); echo "Failed: " . $e->getMessage(); } $dbh = null; } else { $mysqli = new mysqli("localhost", "root", "", "dbmeotest"); if (mysqli_connect_errno()) { $dbmeo_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/database_install.php'; header('Location: ' . $dbmeo_url); } $db_server = mysqli_connect("localhost", "root", ""); if (!$db_server) die("Unable to connect to MySQL: " . mysqli_error($db_server)); mysqli_select_db($db_server, "dbmeotest") or die("Unable to select database: " . mysqli_error($db_server)); if(isset($_POST['save'])) { $author = $_POST['author']; $title = $_POST['title']; $version = $_POST['version']; $link = $_POST['link']; $reference = $_POST['reference']; $query = "INSERT INTO dbmeotest.article VALUES ('NULL', '$author', '$title', '$version', '$link', '$reference')"; $result = mysqli_query($db_server, $query); if (!$result) die("Database access failed: " . mysqli_error($db_server)); mysqli_close($db_server); } if (isset($_POST['search']) || isset($_POST['next']) || isset($_POST['previous'])){ $id = $_POST['id']; $zus='%'; $author_ = $_POST['author']; $author_ = $author_.$zus; if (isset($_POST['search'])) $query = "SELECT * FROM dbmeotest.article WHERE author LIKE '$author_'"; if($id < 0) $id = 1; if(isset($_POST['next'])) { $id++; $query = "SELECT * FROM dbmeotest.article WHERE id = $id"; } if(isset($_POST['previous'])) { $id--; $query = "SELECT * FROM dbmeotest.article WHERE id = $id"; } $result = mysqli_query($db_server, $query); if (!$result) die("Database access failed: " . mysqli_error($db_server)); elseif (mysqli_num_rows($result)) { $row = mysqli_fetch_row($result); $id = $row[0]; $author = $row[1]; $title = $row[2]; $version= $row[3]; $link= $row[4]; $reference= $row[5]; } else die("<p style='background-color: red'><b> Nothing! </b></p>"); mysqli_close($db_server); } } ?> <div> <h1>List of articles</h1><hr /> </div> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FRMarticle" style="width: 50%; height: 100%"> <div> <fieldset class="rounded"> <legend></legend> <input type="hidden" name="id" id="id"> <label for="author" width="25" class="rounded blue">Author: <input type="text" name="author" id="author" size="88" maxlength="30" placeholder="name" class="rounded" /> </label> <br /> <label for="title" class="rounded blue">Title: <input type="text" name="title" id="title" size="90" maxlength="80" placeholder="title" class="rounded" /> </label> <br /> <label for="version" class="rounded blue">Version : <input type="text" name="version" id="version" size="86" maxlength="12" placeholder="version" class="rounded" /> </label> <br /> <label for="link" class="rounded blue">Link : <input type="text" name="link" id="link" size="89" maxlength="110" placeholder="link" class="rounded" /> </label> <br /> <label for="reference" class="rounded blue">Reference : <input type="text" name="reference" id="reference" size="84" maxlength="20" placeholder="reference" class="rounded" /> </label> <br /> </fieldset> </div> <input type="submit" id="search" name="search" class="rounded blue" value="Search by author"> <input type="submit" id="next" name="next" class="rounded blue" value="Next by ID"> <input type="submit" id="previous" name="previous" class="rounded blue" value="Previous by ID"> <input type="submit" id="save" name="save" class="rounded blue" value="Save"> <input type="submit" id="update" name="update" class="rounded blue" value="Update"> <input type="submit" id="delete" name="delete" class="rounded blue" value="Delete"> </form> <script> document.forms[0].id.value = "<?php echo $id;?>";</script> <script> document.forms[0].author.value = "<?php echo $author;?>";</script> <script> document.forms[0].title.value = "<?php echo $title;?>";</script> <script> document.forms[0].version.value = "<?php echo $version;?>";</script> <script> document.forms[0].link.value = "<?php echo $link;?>";</script> <script> document.forms[0].reference.value = "<?php echo $reference;?>";</script> </body> </html>
/* ***************************************************************************** * FILE: meo1.css * Author: Didier MEO * Licence: free MEO Education * An easy sample for the usage of PHP with mysqli and PDO */ /* IS01.css /* * Created on : 06.07.2014, 08:16:08 * Author : Didier Meo ***************************************************************************** */ .button { display: inline-block; outline: none; cursor: pointer; text-align: center; text-decoration: none; font: 14px/100% Arial, Helvetica, sans-serif; padding: .5em 2em .55em; text-shadow: 0 1px 1px rgba(0,0,0,.3); -ms-border-radius: .5em; -khtml-border-radius: .5em; -webkit-border-radius: .5em; -moz-border-radius: .5em; border-radius: .5em; -ms-border-radius: 0 1px 2px rgba(0,0,0,.2); -khtml-border-radius: 0 1px 2px rgba(0,0,0,.2); -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2); -moz-box-shadow: 0 1px 2px rgba(0,0,0,.2); box-shadow: 0 1px 2px rgba(0,0,0,.2); } .button:hover { text-decoration: none; } .button:active { position: relative; top: 1px; } .blue{ color: #d9eef7; border: solid 1px #0076c3; background: #0095cd; background: -webkit-gradient(linear, left top, left bottom, from(#00cdee), to(#0078c5)); } input.rounded, label.rounded, select.rounded, fieldset.rounded { border: 1px solid #ccc; -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px; -moz-box-shadow: 2px 2px 3px #666; -webkit-box-shadow: 2px 2px 3px #666; box-shadow: 2px 2px 3px #666; font-size: 12px; padding: 4px 7px; outline: 0; -webkit-appearance: none; } input.rounded:focus { border-color: #339933; }
/*------------------------------------------------------------------------------- * FILE: database_install.php * Author: Didier MEO * Licence: free MEO Education * An easy sample for the usage of PHP with mysqli and PDO <html> <head> <title>Database dbmeotest</title> <meta charset="utf-8"> </head> <body> <h1>The database dbmeotest</h1> <?php $mysqli = new mysqli("localhost", "root", ""); if (mysqli_connect_errno()) { echo "The connection failed: ".mysqli_connect_error(); exit(); } $query = "CREATE DATABASE dbmeotest CHARACTER SET utf8 COLLATE utf8_general_ci;"; $query1 = "CREATE TABLE dbmeotest.article (id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, author VARCHAR(30) NOT NULL, title VARCHAR(80) NOT NULL, version VARCHAR(12), link VARCHAR(110), reference VARCHAR(20), PRIMARY KEY (id));"; $query2 = "INSERT INTO dbmeotest.article VALUES (NULL,'Didier MEO','WPF demo part 1: Data binding with Master-Detail and MS Access dataset' ,'2012-03-11' ,'http://www.meo-x.net/net/wpf-data-binding-master-detail-ms-access-dataset.html', 'Ref. 20120311'), " ."(NULL,'MEO Didier','Com interop avec internet explorer (shdocvw, mshtml) en c#' ,'2009-05-30' ,'http://codes-sources.commentcamarche.net/source/50072-com-interop-avec-internet-explorer-shdocvw-mshtml-en-c', 'Ref. 20090530')" . ";"; if ($mysqli->query( $query) === TRUE) { echo"Database dbmeotest created successfully. <br />"; if ($mysqli->query( $query1) === TRUE) { echo"Table article created successfully. <br />"; if ($mysqli->query( $query2) === TRUE) { echo"Data inserted into dbmeotest.article successfully. <br />"; } } else { if($mysqli->query( $query2) === TRUE) { echo"Data inserted into dbmeotest.article successfully. <br />"; echo ' Click here <a href='.'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php'.'>index.php</a>'; } else { echo "The request failed: ".mysqli_connect_error()." | "; echo ' Or click here <a href='.'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php'.'>index.php</a>'; } } } else { if ($mysqli->query( $query1) === TRUE) { echo"Table article created successfully. <br />"; if ($mysqli->query( $query2) === TRUE) { echo"Data inserted into dbmeotest.article successfully. <br />"; echo ' Click here <a href='.'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php'.'>index.php</a>'; } } else if($mysqli->query( $query2) === TRUE) { echo"Data inserted into dbmeotest.article successfully. <br />"; echo ' Click here <a href='.'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php'.'>index.php</a>'; } else { echo "The request failed: ".mysqli_connect_error()." | "; echo ' Or click here <a href='.'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php'.'>index.php</a>'; } } $mysqli->close(); ?> </body> </html>

Tweet