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>";
}