inner join su due tabelle

  • Creatore Discussione Creatore Discussione luigi777
  • Data di inizio Data di inizio

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
43
Massa, Italy
Salve, sto faccendo un query pero la query non funziona.. ho meglio mi dice che non può far girare fetchall di pdo.

Mi dite come posso fare
ecco la query:
PHP:
SELECT blog_categories FROM blog join blog_categories ON blog_categories.id=blog.id_categories WHERE blog.id_categories=".$_GET["id"].""

questo sono le tabelle:
PHP:
CREATE TABLE blog ( 
	id INTEGER  PRIMARY KEY   AUTOINCREMENT,
	id_categories int,
    subject VARCHAR(255) NOT NULL,
    content LONGTEXT,
    is_public int,
    data_reg DATETIME);

CREATE TABLE blog_categories ( 
	id INTEGER  PRIMARY KEY   AUTOINCREMENT,
    categories VARCHAR(50) NOT NULL UNIQUE,
	is_order int,
    is_public int,
    data_reg DATETIME);

idee? e delle ore che non riesco ad capire come fare.

mi potete dare una mano?

grazie.
 
ok, ho quasi fatto ma non so come mai mi da un undifined index cat

ora vi posto il codice anche se è brutto da vedere che poi sistemerò.
PHP:
<?php defined('ENGINE_BLOG') or die("Pagina non accessibile direttamente"); ?>
<?php
include("blog_include.php");
?>

<p class="ph1">Blog</p>
<br /> <br />
<?php
$per_page = $blog_per_page_admin;
$result = $db->query("SELECT blog_categories.categories as cat, *
FROM  blog 
       INNER JOIN blog_categories
          ON blog.id_categories = blog.id_categories WHERE id_categories=".$_GET["id"]."");
$rows = $result->fetchAll();
$total_records = count($rows);
$pages = ceil($total_records / $per_page); 
$page  = (isset ($_GET['page']))  ? (int) $_GET['page'] : 1 ;
$start = ($page - 1) *  $per_page; 
$query = $db->query("SELECT * FROM blog ORDER BY data_reg DESC LIMIT $start , $per_page ");
?>
<table width="100%" class="table_admin">
<tr>
<th>Titolo</th>
<th>Categoria</th>
<th>Data</th>
<th>Modifica</th>
<th>Cancella</th>
</tr>
<?php
while($row = $query->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["subject"]; ?></td>
<td><?php echo $row["cat"]; ?></td>
<td><?php echo "".data_it($row["data_reg"]).""; ?></td>
<td><a href="blog.php?a=edit&id=<?php echo $row['id']; ?>">Modifica</a></td>		
<td><a href="blog.php?a=del&id=<?php echo $row['id']; ?>">Cancella</a></td>
</tr>		
<?php } ?>
</table>
<?php
if ($pages >=  1 && $page <= $pages){
//if ($page>1 && $page <= $pages){$previous=($page -1); echo '<a href="?page=' .$previous. '">Previous</a>';}
for($x=1; $x<=$pages; $x++){ echo ($x == $page) ? ' <strong><a href="?a=list&page='.$x.'">'.$x.'</a></strong> ' : ' <a href="?a=list&page='.$x.'">'.$x.'</a> ';}
//if ($page>1  && $page <= $pages){ $next=($page+1) ; echo '<a href="?page=' .$next. '">Next</a>';}
}

$db = NULL;
?><br />
<div align="center">
[&nbsp;&nbsp;<a href="blog.php?a=insert">Inserici</a>&nbsp;&nbsp;
[&nbsp;&nbsp;<a href="blog.php?a=cat_list">Categorie</a>&nbsp;&nbsp;
[&nbsp;&nbsp;<a href="blog.php?a=logout">Logout</a>&nbsp;&nbsp;]
[&nbsp;&nbsp;<a href="blog.php?a=password">Cambia Password</a>&nbsp;&nbsp;]
</div>

mi date una mano?..
grazie mille.
buona settimana.
 
non capisco perché non stampa nemmeno il var dump..

ha che righa lo devo inserire?

perchè quel codice lo trovato su un sito inglese e lo modificato cambiando solo le tabelle.

Prima senza la inner join funzionava solo che ora messa quella per recuperare il nome della categoria tramite la tabella blog_categories.categories as cat,

non funziona più.

e poi devo mettere manualmente il get alla barra degli indirizzi id=1
come posso evitare ?

grazie mille.
 
ciao, c'è lo fatta...

mi sai dire come faccio fare una select automatica senza che io devo mettere id=4?

ecco il codice come sto provando ad evitare la get.

PHP:
<?php defined('ENGINE_BLOG') or die("Pagina non accessibile direttamente"); ?>
<?php
include("blog_include.php");
?>

<p class="ph1">Blog Admin</p>
<br /> <br />
<?php
$per_page = $blog_per_page_admin;
$id = (isset($_GET["id"])) ? trim ((int)($_GET["id"])): '';

$query ="SELECT  blog_categories.categories as _cat , blog.* FROM blog 
       INNER JOIN blog_categories 
          ON blog_categories.id = blog.id_categories";
if(!empty($_GET['id'])) {
$query.="WHERE blog.is_public=1 and  id_categories=".$id." ORDER BY data_reg DESC";
}

$query.="WHERE blog.is_public=1 ORDER BY data_reg DESC";
$result = $db->query($query);
$rows = $result->fetchAll(); 

$total_records = count($rows);
$pages = ceil($total_records / $per_page); 
$page  = (isset ($_GET['page']))  ? (int) $_GET['page'] : 1 ;
$start = ($page - 1) *  $per_page; 
$query = $db->query("SELECT  blog_categories.categories as _cat , blog.* FROM blog 
       INNER JOIN blog_categories 
          ON blog_categories.id = blog.id_categories WHERE blog.is_public=1 and  id_categories=".$id." LIMIT $start , $per_page ");
?>
<table width="100%" class="table_admin">
<tr>
<th>Titolo</th>
<th>Categoria</th>
<th>Data</th>
<th>Modifica</th>
<th>Cancella</th>
</tr>
<?php
while($row = $query->fetch()){
var_dump($row)
?>
<tr>
<td><?php echo $row["subject"]; ?></td>
<td><?php echo $row["_cat"]; ?></td>
<td><?php echo "".data_it($row["data_reg"]).""; ?></td>
<td><a href="blog.php?a=edit&id=<?php echo $row['id']; ?>">Modifica</a></td>		
<td><a href="blog.php?a=del&id=<?php echo $row['id']; ?>">Cancella</a></td>
</tr>		
<?php } ?>
</table>
<?php
if ($pages >=  1 && $page <= $pages){
if ($page>1 && $page <= $pages){$previous=($page -1); echo '<a href="?page=' .$previous. '">Previous</a>';}
for($x=1; $x<=$pages; $x++){ echo ($x == $page) ? ' <strong><a href="?a=list&page='.$x.'">'.$x.'</a></strong> ' : ' <a href="?a=list&page='.$x.'">'.$x.'</a> ';}
if ($page>1  && $page <= $pages){ $next=($page+1) ; echo '<a href="?page=' .$next. '">Next</a>';}
}

$db = NULL;
?><br />
<div align="center">
[&nbsp;&nbsp;<a href="blog.php?a=insert">Inserici</a>&nbsp;&nbsp;
[&nbsp;&nbsp;<a href="blog.php?a=cat_list">Categorie</a>&nbsp;&nbsp;
[&nbsp;&nbsp;<a href="blog.php?a=logout">Logout</a>&nbsp;&nbsp;]
[&nbsp;&nbsp;<a href="blog.php?a=password">Cambia Password</a>&nbsp;&nbsp;]
</div>

grazie mille.
 
Ultima modifica:

Discussioni simili