DAVID

Berruezo

Graduat Multimedia UPC

CITM | Grau Multimèdia | UPC Barcelona

Candidate Computer Engineering UOC

Programador / Consultor / Formador / Startups / Visión Comercial

Visit how to get ALL products, comments, images ALL from BEST business as coches.net, milanuncios, shop online here as vinoseleccion

https://davidberruezo.com/davidberruezo/public_html/blog/../images/cuadro1.jpg
https://davidberruezo.com/davidberruezo/public_html/blog/../images/cuadro2.jpg
https://davidberruezo.com/davidberruezo/public_html/blog/../images/cuadro3.jpg
https://davidberruezo.com/davidberruezo/public_html/blog/../images/cuadro4.jpg

Pdo transaction example create table and insert rows

Example show how to create a pdo transaction to create table and insert rows …

1.- Create two string for example by EOT

/*
 * Creamos las sentencias
 * sql create table
 * e insert
 *
 */
$create = <<<EOT
    CREATE TABLE Customers(
    CustomerId 	 int(11) NOT NULL auto_increment PRIMARY KEY,
	Name 		 varchar(255) NOT NULL default "",
	Address 	 varchar(255) NOT NULL default "",
	City 		 varchar(255) NOT NULL default "",
	State 		 varchar(255) NOT NULL default "",
	Zip 		 varchar(255) NOT NULL default "",
	Phone 	     varchar(255) NOT NULL default "",
	Email 		 varchar(255) NOT NULL default ""
) ENGINE = INNODB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT="Customer Data";
EOT;

$insert = <<<EOT
    INSERT INTO `Customers` VALUES (1, 'Michael Smith', '123 Somewhere Road', 'Beverly Hills', 'California', '90210', '(555) 555-1234', 'michael@somewhere.com');
    INSERT INTO `Customers` VALUES (2, 'Matthew Johnson', '1234 Somewhere Else Street', 'Elsewhere', 'Confusion', '00000', '(555) 555-2345', 'johnboy@neato.net');
    INSERT INTO `Customers` VALUES (3, 'Cindy Benjamin', '1313 Mockingbird Lane', 'Somewhere', 'Montana', '00000', '(555) 555-9876', 'cindybean@mcok.net');
    INSERT INTO `Customers` VALUES (4, 'Mary Klein', '10 Highland Avenue', 'Salem', 'Massachusetts', '01970', '(555) 555-4920', 'mary@klein.net');
EOT;

2.- Create transaction sentences ..

/*
 * Conectamos a la bd
 */
try {
    $usuario    = "root";
    $contraseña = "Berruezin23";
    $mbd        = new PDO('mysql:host=localhost;dbname=ajaxlibro', $usuario, $contraseña);
    $mbd->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    print "¡Error!: " . $e->getMessage() . "<br/>";
    die();
}

/*
 * Hacemos una transacción de la
 * la creación de la tabla y los inserts
 * a la tabla
 */
try{
    $mbd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $mbd->beginTransaction();
    $mbd->exec($create);
    $mbd->exec($insert);
    $mbd->commit();
} catch (PDOException $e){
    $mbd->rollBack();
    echo "Fallo: " . $e->getMessage();
}

Mysql joins example

To make Mysql joins you’ll do .. for example

SELECT pr.id,pr.titulo,pr.idCategoria,pi.id as idimagen, pi.imagen,pc.categoria,pd.proyectoDesc,e.nombre as nombreEmpresa,pi.portada
FROM proyectos as pr
JOIN proyectos_imagenes as pi
ON pr.id = pi.id_proyecto
JOIN proyectos_categorias as pc
ON pr.idCategoria = pc.id
JOIN proyectos_desc as pd
ON pr.id = pd.id_proyecto
JOIN proyectos_empresas as pe
ON pe.id = pd.id_proyecto
JOIN empresas as e
ON e.id = pe.id_empresa
where pd.id_idioma=2 and pi.portada = 1;

Mysql add column

To add column to table you’ll do

SET SQL_SAFE_UPDATES = 0;
ALTER TABLE prueba add COLUMN `imagen` int(10);

Pdo transactions

Ho to make pdo transactions

try {
    $usuario    = "root";
    $contraseña = "Berruezin23";
    $dbh = new PDO('mysql:host=localhost;dbname=davidber_web', $usuario, $contraseña,array(PDO::ATTR_PERSISTENT => true));
    /*
    var_dump($mbd);
    echo("<br>");
    foreach($mbd->query('SELECT * from album') as $fila) {
        print_r($fila);
    }
    $mbd = null;
    */
} catch (PDOException $e) {
    print "¡Error!: " . $e->getMessage() . "<br/>";
    die();
}

try{
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->beginTransaction();
    $dbh->exec("insert into album (id, artist, title) values (1, 'Artista', 'Artista Album')");
    $dbh->exec("insert into prueba (id, nombre) values (1, 'Artista')");
    $dbh->commit();
} catch (PDOException $e){
    $dbh->rollBack();
    echo "Fallo: " . $e->getMessage();
}

/*
$dbh->beginTransaction();
$query = "SELECT yourFunction(0::smallint,'2013-08-01 00:00','2013-09-01 00:00',1::smallint,'$cursor')";
$dbh->query($query);
$query = "FETCH ALL IN \"$cursor\"";
echo "begin data<p>";
foreach ($dbh->query($query) as $row)
{
    echo "$row[0] $row[1] $row[2] <br>";
}
echo "end data";
*/