获取返回NULL名称的标签名称的函数

时间:2016-03-21 20:01:14

标签: php mysql pdo

我有两个简单的表来存储项目的标签:

标签:statement = "SELECT * FROM table" if filterList: statement += " WHERE column IN ({0})".format( ', '.join(['%s'] * len(filterList))) cursor.execute(statement, filterList)

id | tag是名称,因此每一行都是唯一的。

showcase_tags:tag

我根据项目的ID获取标签,如下所示:

id | tag_id | showcase_id

showcase类中的函数:

$tags = $showcase->getTags($id)

public function getTags ($id) { $stmt = $this->db->getPdo()->prepare( "SELECT tag_id FROM showcase_tags WHERE showcase_id = :id "); $stmt->bindParam(":id", $id); $stmt->execute(); $tagIds = $stmt->fetchAll(); return $this->getTagNames($tagIds); } private function getTagNames ($tagIds) { $tagNames = []; foreach ($tagIds as $id){ $stmt = $this->db->getPdo()->prepare( "SELECT tag FROM tags WHERE id = :id "); $stmt->bindParam(":id", $id); $stmt->execute(); $name = $stmt->fetch()['tag']; array_push($tagNames, $name); } return $tagNames; } 数组$tagNames只包含$tags = $showcase->getTags($id)而不是标记的名称,但项目具有正确的标记数。

0 个答案:

没有答案