PHP & MySQL

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 24 jul 2019 om 07:54 (→‎Fetch data)
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen

Tenzij anders aangegeven, gebruikt dit artikel PDO voor database-connecties.

Drivers

  • MySQLi en PDO lijken anno 2019 de twee belangrijkste drivers/API's te zijn
  • mysql_ is oud en begraven
  • Daarnaast bestaan er ook voor PHP ORM's

PDO

  • Generiek voor diverse databases - Boeit niet, want ik gebruik momenteel alleen MySQL
  • Niet zo up-to-date als MySQLi
  • "A really nice thing with PDO is you can fetch the data, injecting it automatically in an object. If you don't want to use an ORM (cause it's a just a quick script) but you do like object mapping, it's REALLY cool" [1]

MySQLi

  • Meer up-to-date
  • Primitiever

Conclusie (april 2019)

  • [2] (juni 2018): PDO
  • [3] (feb. 2012): PDO
  • [4] (2003): PDO

Connect (PDO)

Voorbeeld:

<?php

###############################################################
# Connect with dwh through PDO
###############################################################
#
$servername = "localhost";
$dbname     = "example";
$username   = "supervrouw";
$password   = "geheim";

try 
{
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    
    # set the PDO error mode to exception
    #####################################
    #
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully\n"; 
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage()."\n";
}

Fetch data

Soms, soms is het leven verrassend eenvoudig [5]:

###############################################################
# Fetch & process table "product_tmp"
###############################################################
#
$sql = $conn->prepare("select * from product_tmp");
$sql->execute();
$rows = $sql->fetchAll();

foreach ($rows as $row)
{
	echo $row['sku']."\n";
}

Write data

Tjakka:

<?php
#
# Test writing to a database
###############################################################
#
echo "\n\n";
echo "###############################################################\n";
echo "# Test-writing-to-a-database - Start ##########################\n";
echo "###############################################################\n\n";


###############################################################
# Set variables
###############################################################
#
$db_name	=	"example";
$db_user	=	"example";
$db_password	=	"xxxxxxx";


###############################################################
# Load libraries
###############################################################
#
echo "Load libraries...\n";
#
// require_once($site_path . "/wp-load.php");
// require_once($site_path . '/wp-admin/includes/media.php');
// require_once($site_path . '/wp-admin/includes/file.php');
// require_once($site_path . '/wp-admin/includes/image.php');
require_once('/home/strompf/Dropbox/Scripts/php/dvb_db_functions.php');
# require_once('/home/strompf/Dropbox/Scripts/php/dvb_wordpress_functions.php');

echo "Load libraries...Done\n";


###############################################################
# Connect with dwh
###############################################################
#
echo "Connect with dwh...\n";
#
$conn = dvb_create_pdo_object($db_name, $db_user, $db_password);

echo "Connect with dwh...Done\n";


###############################################################
# Execute query
###############################################################
#
echo "Execute query...\n";

$sql_string="update ean_tmp set sku = concat(sku, '_blub');";

$sql=$conn->prepare($sql_string);
$sql->execute();

echo "Execute query...Done\n";

Tekencodering

Argh! Duitse umlauten gaan om zeep als je niet oppast. De oplossing: In de connection string aangeven wat de tekencodering is, die je in de db gebruikt (en als die verschilt van tabel tot tabel? Geen idee). Voorbeeld [6]:

$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);