Soluciones examen 2

/*
 * * MySQLi procedural * MySQLi orientat a objectes * PDO
 * 
 * MySql MySqli PDO
 */


$sql = "insert into clientes (mail,password) values (:mail,:password)";
$st = $conn->prepare($sql);
$st->execute(array(':mail' => 'pepe@pepe.com', ':password' => '1234'));


$sql = "SELECT count(idproductos) as total FROM productos";
$st = $conn->prepare($sql);
$st->execute();
$row = $st->fetch();
echo "El número de productos es: " . $row['total'] . "<br/>";


$sql = "SELECT sum(importe) as total FROM pedido";
$st = $conn->prepare($sql);
$st->execute();
$row = $st->fetch();
echo "La suma de los importes de los pedidos es: " . $row['total'] . "<br/>";


$sql = "delete from productos where idproductos=:idproductos";
$st = $conn->prepare($sql);
$st->execute(array(':idproductos' => 5));

$sql = "SELECT mail FROM clientes";
$st = $conn->prepare($sql);
$st->execute();
while ($row = $st->fetch()) {
 echo $row['mail'] . "<br/>";
}

$sql = "SELECT mail FROM clientes where mail like '%@gmail.com'";
$st = $conn->prepare($sql);
$st->execute();
while ($row = $st->fetch()) {
 echo $row['mail'] . "<br/>";
}


$sql = "insert into clientes (mail, password) values (:mail, :password)";
$st = $conn->prepare($sql);
for ($i = 0; $i < 10; $i++)
 $st->execute(array(':mail' => 'test@test.com', ':password' => '1234'));

function import_total_IVA($idclient) {
 global $conn;
 try {
 $sql = "select sum(import*(1+iva)) as total from comandes join clients using (idclients) where idclients=:idclients";
 $stmt = $conn->prepare($sql);
 $stmt->execute(array(':idclients' => $idclient));
 $fila = $stmt->fetch();
 $total = $fila['total'];
 return $total;
 } catch (Exception $ex) {
 return 0;
 }
}

Soluciones examen 1

//56

function lamaslarga($cad1, $cad2) {
 if (strlen($cad1) > strlen($cad2)) {
 return $cad1;
 } else {
 return $cad2;
 }
}

include_once 'funciones.php';

$f = fopen(‘alumnos.txt ’, ‘r’);

$colores = array("rojo", "naranja", "amarillo", "verde", "cian", "azul", "violeta");

if ($resultado) {
 ?> <h1>Bien</h1> <?php } else { ?> <h2>Mal</h2> <?php
}

function invertir_cad($tabla) {
 for ($i = 0; $i < count($tabla); $i++) {
 $tabla2[$i] = strrev($tabla[$i]);
 } return $tabla2;
}

function tablero($numCeldas) {
 $num = 0;
 echo "<table border=1>";
 for ($i = 0; $i < $numCeldas; $i++) {
 echo "<tr>";
 for ($j = 0; $j < $numCeldas; $j++) {
 echo "<td>" . ($i + $j) . "</td>";
 } 
 echo "</tr>";
 } echo "</table>";
}

SQL ejercicio tienda

CREATE TABLE `tienda`.`productos` (
 `idproductos` INT NOT NULL AUTO_INCREMENT,
 `nombre` VARCHAR(50) NULL,
 `precio` DECIMAL(5,2) NULL,
 `descripcion` VARCHAR(400) NULL,
 PRIMARY KEY (`idproductos`));
 
 CREATE TABLE `tienda`.`clientes` (
 `idclientes` INT NOT NULL AUTO_INCREMENT,
 `mail` VARCHAR(100) NULL,
 `password` CHAR(32) NULL,
 `direccion` VARCHAR(200) NULL,
 `cpostal` CHAR(5) NULL,
 `ciudad` VARCHAR(50) NULL,
 PRIMARY KEY (`idclientes`));
 
 CREATE TABLE `tienda`.`pedidos` (
 `idpedidos` INT NOT NULL AUTO_INCREMENT,
 `idclientes` INT NULL,
 `fecha` DATETIME NULL,
 `importe` DECIMAL(6,2) NULL,
 `iva` DECIMAL(2,2) NULL,
 PRIMARY KEY (`idpedidos`));

 CREATE TABLE `tienda`.`pedidos_productos` (
 `idpedidos_productos` INT NOT NULL AUTO_INCREMENT,
 `idpedidos` INT NULL,
 `idproductos` INT NULL,
 `precio` DECIMAL(5,2) NULL,
 `cantidad` INT NULL,
PRIMARY KEY (`idpedidos_productos`)); ALTER TABLE `tienda`.`pedidos_productos` ADD INDEX `fk_productos_idx` (`idproductos` ASC); ALTER TABLE `tienda`.`pedidos_productos` ADD CONSTRAINT `fk_productos` FOREIGN KEY (`idproductos`) REFERENCES `tienda`.`productos` (`idproductos`) ON DELETE RESTRICT ON UPDATE NO ACTION; ALTER TABLE `tienda`.`pedidos_productos` ADD INDEX `fk_pedidos_idx` (`idpedidos` ASC); ALTER TABLE `tienda`.`pedidos_productos` ADD CONSTRAINT `fk_pedidos` FOREIGN KEY (`idpedidos`) REFERENCES `tienda`.`pedidos` (`idpedidos`) ON DELETE RESTRICT ON UPDATE NO ACTION; ALTER TABLE `tienda`.`pedidos` ADD INDEX `fk_clientes_idx` (`idclientes` ASC); ALTER TABLE `tienda`.`pedidos` ADD CONSTRAINT `fk_clientes` FOREIGN KEY (`idclientes`) REFERENCES `tienda`.`clientes` (`idclientes`) ON DELETE RESTRICT ON UPDATE NO ACTION;