简体   繁体   中英

undefined index by selecting data from two tables with a common column

I have two tables - home and posts ; home.artid is equal to posts.id .

Want select id, artid, inde from home PLUS title from posts
where home.pos is slider :

$items = "";
$st = $db->query("select home.id, home.artid, home.inde 
                    from home 
                        join posts on home.artid = posts.id 
                    where home.pos = 'slider' 
                    order by home.inde asc");
while ($row = $st->fetch()){
    $items .= "<div class='slidertitle'>" . $row['posts.title'] . "</div>\n";
}
echo $items;

Error:
Undefined index posts.title...

Any help?

If you want to select posts.title then select it

$st = $db->query("select home.id, home.artid, home.inde, 
                        posts.title
                from home 
                    join posts on home.artid = posts.id 
                where home.pos = 'slider' 
                order by home.inde asc");

// and it would be called just `title` 

while ($row = $st->fetch()){
    $items .= "<div class='slidertitle'>" . $row['title'] . "</div>\n";
}
echo $items;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM