在Zend Framework MySQL查询中使用PHP数组

时间:2022-09-24 00:18:01

I am trying to build a tag system and I would like to query my table of tags before uploading the new tags to weed out duplicate tags. So I package the tags in an exploded array, and when I go to query the table with the array I get numerous error messages from Zend about how it can't convert the array to a string.

我正在尝试构建一个标记系统,我想在上传新标记之前查询我的标记表以清除重复的标记。所以我将标签打包在一个爆炸数组中,当我用数组查询表时,我从Zend获得了大量关于如何将数组转换为字符串的错误消息。

This is what I have so far.

这就是我到目前为止所拥有的。

$tags = explode(', ', $_POST['itemTag']);

foreach($tags as $tag) {
    $tag_sql[] = '\''.$tag.'\'';
}
$tag_where = implode(',',$tag_sql);

$i = 0;
while ($i < count($tags)) {


    $sql = $dbRead->quoteInto("SELECT tagID FROM item_tag WHERE tag IN ($tag_where)");
$tag_result = $dbRead->fetchAll($sql);

    if ($tag_result) {
        $tag_ID = $tag_result;
    }
    else {
    $data = array ('tag' => $tags[$i]);
    $dbWrite->insert('item_tag', $data);
    $tag_ID = $dbWrite->lastInsertId();  }

$data = array('itemID' => $item_ID,
              'tagID'   => $tag_ID);

    $dbWrite->insert('item_tag_connection', $data);
++$i;
    }
}

I also need it to insert the ID number of the tag if one is found during the query into the connecting table and I am unsure if the code I have is functional as I can't get past the array query problem.

我还需要它插入标签的ID号,如果在查询到连接表期间找到一个,我不确定我的代码是否有用,因为我无法通过数组查询问题。

Thank you.

谢谢。

2 个解决方案

#1


3  

Let Zend quote the array for you like that, using quoteInto():

让Zend使用quoteInto()为你引用数组:

$dbRead->quoteInto('SELECT tagID FROM item_tag WHERE tag IN (?)', $tags)

#2


0  

try this :

尝试这个 :

$tags = explode(', ', $_POST['itemTag']);
foreach($tags as $tag)
{
    $sql = $dbRead->quoteInto("SELECT tagID FROM item_tag WHERE tag='$tag'");
    $tag_result = $dbRead->fetch($sql);

    if ($tag_result) { // I'm not sure if you should use this or count($tag_result) > 0
        $tag_ID = $tag_result['tagID'];
    } else {
        $data = array ('tag' => $tag);
        $dbWrite->insert('item_tag', $data);
        $tag_ID = $dbWrite->lastInsertId();  
    }

    $data = array('itemID' => $item_ID,
              'tagID'   => $tag_ID);

    $dbWrite->insert('item_tag_connection', $data);
}

#1


3  

Let Zend quote the array for you like that, using quoteInto():

让Zend使用quoteInto()为你引用数组:

$dbRead->quoteInto('SELECT tagID FROM item_tag WHERE tag IN (?)', $tags)

#2


0  

try this :

尝试这个 :

$tags = explode(', ', $_POST['itemTag']);
foreach($tags as $tag)
{
    $sql = $dbRead->quoteInto("SELECT tagID FROM item_tag WHERE tag='$tag'");
    $tag_result = $dbRead->fetch($sql);

    if ($tag_result) { // I'm not sure if you should use this or count($tag_result) > 0
        $tag_ID = $tag_result['tagID'];
    } else {
        $data = array ('tag' => $tag);
        $dbWrite->insert('item_tag', $data);
        $tag_ID = $dbWrite->lastInsertId();  
    }

    $data = array('itemID' => $item_ID,
              'tagID'   => $tag_ID);

    $dbWrite->insert('item_tag_connection', $data);
}