可捕获的致命错误:无法转换为字符串


Catchable fatal error: cannot be converted to string?

我无法解决以下问题

"Catchable fatal error: Object of class mysqli_result could not be converted to string."

我有3张桌子:照片(photo_id,photo_filename,photo_caption),标签(tag_id,tag_title)
其中tag_title是唯一的,并且photos_tags(photo_id,tag_id)。

用户可以添加描述照片的标签(他可以根据需要插入任意数量的标签,用逗号分隔它们)。对于每个标签,我想检查它是否已经存在,以便避免表photos_tags中的重复。它适用于桌子照片和标签,但我无法photos_tags填满桌子。任何帮助将不胜感激。

$tags = $tag_title;
$e = explode(',', $tags);
foreach($e as $value) //for every tag
{
    $q_photo_id = mysqli_query($con, "SELECT photo_id FROM photos WHERE 
    (photo_filename ='$photo_filename' AND photo_caption = '$photo_caption')");
    $photo_id = mysqli_fetch_array($q_photo_id);
    $sql4 = mysqli_query($con, "INSERT IGNORE INTO tags (tag_title) VALUES ('$value')");
    if (mysqli_insert_id($con)) {
        $q_tag_id = mysqli_query($con, "SELECT tag_id FROM tags WHERE ('tag_title' = '$value')");
        $tag_id = mysqli_fetch_array($q_tag_id);
    } 
    else { //if the tag already exist
        $q_tag_id = mysqli_query($con, "SELECT tag_id FROM tags WHERE ('tag_title' = '$value')");
        $tag_id = mysqli_fetch_array($q_tag_id);
    }
 // insert into photos_tags
     $sql6 = mysqli_query($con, "INSERT INTO photos_tags (photo_id, tag_id) VALUES ('$photo_id', '$tag_id')");
    }
}   

我不明白为什么调试这是一个问题。您应该插入字符串或可以转换为字符串的内容。在这里,您使用的是$q_photo_id$q_tag_id两者都是mysqli_result对象。它们不能被转换为字符串,PHP 理所当然地抱怨同样的事情。您应该改用$photo_id[0]$tag[0]