generated from francesco/deploy-dinamico
Conversione da MySQLi a PDO - pt. 1
All checks were successful
Deploy / trigger (push) Successful in 17s
All checks were successful
Deploy / trigger (push) Successful in 17s
This commit is contained in:
@@ -6,37 +6,48 @@ if (isset($_GET['s'])) {
|
||||
}
|
||||
|
||||
// Ricerca film dal DB
|
||||
// Questi valori vengono letti automaticamente
|
||||
$host = getenv('DB_HOST'); // Sarà "local_db"
|
||||
$db = getenv('DB_NAME'); // Sarà "nomerepository-nomeutente"
|
||||
$user = getenv('DB_USER'); // Uguale al nome DB
|
||||
$pass = getenv('DB_PASS'); // Password generata casualmente ad ogni deploy
|
||||
$charset = 'utf8mb4';
|
||||
|
||||
$conn = mysqli_connect("localhost", "root", "", "film_attori");
|
||||
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
||||
|
||||
if ($conn === false) {
|
||||
exit("<code>Errore: impossibile stabilire una connessione " . mysqli_connect_error() . "</code>");
|
||||
try {
|
||||
$pdo = new PDO($dsn, $user, $pass);
|
||||
} catch (\PDOException $e) {
|
||||
throw new \PDOException($e->getMessage(), (int) $e->getCode());
|
||||
}
|
||||
|
||||
// Collegamento al DB effettuato
|
||||
|
||||
$query = "SELECT film.id_film, film.nome, film.trama, film.data_uscita, generi.nome as genere FROM film, generi WHERE film.id_genere = generi.id_genere ORDER BY film.data_uscita";
|
||||
$searched = "";
|
||||
|
||||
// Leggo la ricerca
|
||||
if (isset($_GET['q'])) {
|
||||
if (is_string($_GET['q']) && $_GET['q'] !== "" && $_GET['q'] !== null) {
|
||||
$nome = htmlspecialchars($_GET['q']);
|
||||
$searched = $nome;
|
||||
$query = "SELECT film.id_film, film.nome, film.trama, film.data_uscita, generi.nome as genere FROM film, generi WHERE film.id_genere = generi.id_genere AND film.nome LIKE '%" . $nome . "%' ORDER BY film.data_uscita";
|
||||
}
|
||||
$params = [];
|
||||
|
||||
if (isset($_GET['q']) && $_GET['q'] !== '') {
|
||||
$nome = $_GET['q'];
|
||||
$searched = $nome;
|
||||
|
||||
$query = `SELECT film.id_film, film.nome, film.trama, film.data_uscita, generi.nome as genere
|
||||
FROM film
|
||||
JOIN generi ON film.id_genere = generi.id_genere
|
||||
WHERE film.nome LIKE :nome
|
||||
ORDER BY film.data_uscita`;
|
||||
|
||||
$params[':nome'] = "%$nome%";
|
||||
} else {
|
||||
$query = `SELECT film.id_film, film.nome, film.trama, film.data_uscita, generi.nome as genere
|
||||
FROM film
|
||||
JOIN generi ON film.id_genere = generi.id_genere
|
||||
ORDER BY film.data_uscita`;
|
||||
}
|
||||
|
||||
// Leggo i dati del film e faccio il JOIN per avere il nome del genere
|
||||
$res = mysqli_query($conn, $query);
|
||||
|
||||
if ($res === false) {
|
||||
// Codice HTTP 500: Errore
|
||||
http_response_code(500);
|
||||
echo "<code>Errore: impossibile eseguire la query. " . mysqli_error($conn) . "</code>";
|
||||
mysqli_close($conn);
|
||||
exit();
|
||||
}
|
||||
$stmt = $pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
|
||||
?>
|
||||
|
||||
@@ -71,7 +82,8 @@ if ($res === false) {
|
||||
<h1>Film in sala</h1>
|
||||
<form action="." method="GET">
|
||||
<fieldset>
|
||||
<input type="text" id="query" name="q" value="<?php echo $searched; ?>" placeholder="Scrivi il titolo...">
|
||||
<input type="text" id="query" name="q" value="<?php echo $searched; ?>"
|
||||
placeholder="Scrivi il titolo...">
|
||||
<button type="submit">Cerca</button>
|
||||
</fieldset>
|
||||
</form>
|
||||
@@ -84,12 +96,13 @@ if ($res === false) {
|
||||
|
||||
$test = true;
|
||||
|
||||
while ($row = mysqli_fetch_array($res)) {
|
||||
while ($row = $stmt->fetch()) {
|
||||
$test = false;
|
||||
?>
|
||||
<a href="<?php echo "../film?id=" . $row['id_film']; ?>">
|
||||
<div class="film">
|
||||
<img src="<?php echo "../api/v1/img/?n=" . urlencode($row['nome'] . ' ' . date('Y', strtotime($row['data_uscita']))); ?>">
|
||||
<img
|
||||
src="<?php echo "../api/v1/img/?n=" . urlencode($row['nome'] . ' ' . date('Y', strtotime($row['data_uscita']))); ?>">
|
||||
<h5><?php echo $row['nome']; ?></h5>
|
||||
<div class="genere">
|
||||
<?php echo $row['genere']; ?>
|
||||
@@ -99,9 +112,6 @@ if ($res === false) {
|
||||
<?php
|
||||
}
|
||||
|
||||
mysqli_free_result($res);
|
||||
mysqli_close($conn);
|
||||
|
||||
?>
|
||||
</div>
|
||||
<?php
|
||||
|
||||
Reference in New Issue
Block a user