inner join su due tabelle

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
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.
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
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.
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
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.
 

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
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
Autore Titolo Forum Risposte Data
L Soluzione con INNER JOIN Database 0
K Problema Inner join PHP 1
L Problema con inner join PHP 11
K [php] Problema con inner join PHP 4
S [PHP] Inner join su 4 tabelle PHP 6
M [PHP] Aiuto su inner join PHP 10
P [MySQL] Inner Join o Left Join MySQL 14
simgia [MySQL] INNER JOIN problema MySQL 0
R [MySQL] SQL UPDATE INNER JOIN MySQL 5
jdj78 inner join Classic ASP 18
D Problema sintassi INNER JOIN a 3 tabelle PHP 4
L inner join MySQL 0
I Aiuto INNER JOIN MySQL 5
A Count Inner Join e stampa record con id non duplicati PHP 1
P Inner join ? MS Access 1
L Inner Join con più tabelle PHP 5
A Inner join con tre tabelle PHP 0
c.ronaldo Inner join Database 1
cosov Problema con INNER JOIN Classic ASP 1
S Stampare il risultato di una query inner jon in php.... PHP 4
metalgemini codice dinamico con inner.HTML o cosa? Javascript 8
M LEFT JOIN di diverse condizioni PHP 10
R Join articolato su 3 tabelle MySQL 5
M Join 3 tabelle PHP 0
L select join e sottrazione punti PHP 13
M Problema su query JOIN in tre tabelle PHP 0
MarcoGrazia Dati nulli su join tra più tabelle MySQL 1
M Ordinare una query join PHP 2
M Estrarre valori MAX da un db con una left join MySQL 8
P Query Mysql con Join PHP 2
F [MySQL] Problema con LEFT JOIN MySQL 6
simgia [PHP] Ciclare una query JOIN PHP 2
R [PHP] Estrapolare dati da una query left join PHP 0
C Php/Mysql query JOIN tra tabelle PHP 4
C Php/Mysql query JOIN tra due tabelle PHP 18
G [MS Access] JOIN di due tabelle più GROUP BY MS Access 1
G [MySQL] Join o select MySQL 1
R [MySQL] Join, Count e Order (ASC & DESC)... MySQL 6
B Outer join che non funge Database 2
V [Guida]: ho dubbi sulle Join MySQL 13
P Query con Somma di un Campo , aggregazione e join Database 3
V Quando mettere il JOIN e quando no? MySQL 1
L cinque tabelle e join PHP 2
P Join tabelle MySQL 4
Marco_88 Select distinct e join MySQL 2
Marco_88 Eliminare una colonna da una LEFT JOIN MySQL 4
M Query Join per 2 colonne MySQL 1
andrea.peo Problema query con join visualizzazione record ripetuti PHP 5
D Il JOIN che non imparerò mai ad usare MySQL 2
V Problema JOIN SQL PHP 3

Discussioni simili