ciao a tutti ... volevo chiedervi un aiuto su questa query MYSQL.
Piu che altro vorrei avere una spiegazione chiara perche non riesco a capire l' utilita.
Secondo un manuale da cui sto studiando, in questo capitolo, preferisce utilizzare le query in questa maniera. Vi riporto il codice.
Non capisco perchè inizializza le query $select $from $where;
Per esempio $where .= ' AND authorid = :authorid';
anche perche la query non sarebbe incompleta iniziando con il WHERE ?
grazie in anticipo
codice:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Authors</title>
</head>
<body>
<h1>Gestione Autori</h1
<?php include $_SERVER['DOCUMENT_ROOT'] . '/FILE_MAGIC/conn2.php';
try{
$autori = 'SELECT id,name from author';
$esegui_autori = $pdo->query($autori);
}
catch(PDOException $e)
{
$errore = 'Errore selezione valori da tabella autori : ' . $e->getMessage();
include 'errore.php';
exit();
}
foreach($esegui_autori as $row)
{
$autori_[] = array('id'=>$row['id'], 'name'=>$row['name']);
}
try{
$categorie = 'SELECT id,name from category';
$esegui_categorie = $pdo->query($categorie);
}
catch(PDOException $e)
{
$errore = 'Errore selezione valori da tabella categoria : ' . $e->getMessage();
include 'errore.php';
exit();
}
foreach($esegui_categorie as $row)
{
$categorie_[] = array('id'=>$row['id'], 'name'=>$row['name']);
}
include 'form_cerca.php';
if(isset($_GET['action']) and $_GET['action'] == 'search')
{
$select = 'SELECT id,joketext';
$from = ' FROM joke';
$where = ' WHERE TRUE';
$memorizza = array();
if($_GET['author_select'] != '')// se è stato selezionato un autore
{
$where .= ' AND authorid = :authorid';
$memorizza[':authorid'] = $_GET['author_select'];
}
if($_GET['category_select'] != '')// se è stato selezionato un autore
{
$from .= ' INNER JOIN jokecategory ON id = jokeid';
$where .= ' AND categoryid = :categoryid';
$memorizza[':categoryid'] = $_GET['category_select'];
}
if($_GET['text'] != '')// se è stato selezionato un autore
{
$where .= ' AND joketext LIKE :joketext';
$memorizza[':joketext'] = '%' . $_GET['text'] . '%';
}
try{
$sql_concatenata = $select . $from . $where;
$esegui = $pdo->prepare($sql_concatenata);
$esegui->execute($memorizza);
}
catch (PDOException $e)
{
$errore = 'ERRORE!!!!!!!!' . $e->getMessage();
include 'errore.php';
exit();
}
}
foreach($esegui as $row)
{
$visualizza[] = array('id'=>$row['id'], 'text'=>$row['joketext']);
}
include 'form_joke.php';
?>