我的mySQL数据库中有一个"附加图像"列,我正试图(到目前为止没有成功)将第一个URL复制到另一列作为"主图像"
例如:
http://website.com/img1.jpg,http://website.com/img2.jpg,http://website.com/img3.jpg,etc..
这些图像周围没有引号,数据库是通过提要自动更新的,所以简单地手动更新每个图像是不可能的。
我有一些PHP代码:
$query5 = "SELECT COL_66 FROM tbl_name";
$result6 = mysql_query($query5);
if (!$result6) {
echo 'Query5 Failed: ' . mysql_error();
exit;
}
$row2 = mysql_fetch_row($result6);
我试着用
$picture = implode(",", $row2);
echo $picture[0];
然后我尝试了
$picture = explode(",", $row2);
echo $picture[0];
内爆的回报是:
h
爆炸的回报是:
Warning: explode() expects parameter 2 to be string, array given in ...
我假设这是因为img URL的(?)周围没有引号
我做错什么了吗?这和引号有关系吗?
感谢阅读&任何帮助!
您使用的是implode()
。将字符串数组连接为一个字符串。
你需要相反的。。。explode()
,它获取一个字符串并将其拆分为一个数组。
你需要循环浏览你的结果集,如下所示:
while ($row2 = $mysql_fetch_row($result6)) {
// assuming the column you need is the first column returned by the query
$picture = explode(',', $row2[0]);
echo $picture[0];
// OR
list($picture) = explode(',', $row2[0]);
echo $picture;
}
正如@zerkms在问题评论中所说,你应该以不同的方式存储它。在我看来,这是一种一对多的关系,所以您应该将这些URL存储在一个单独的表中。类似这样的东西:
+-------------+-----------------------------+---------+
| mainTableID | URL | primary |
+-------------+-----------------------------+---------+
| 1 | http://website.com/img1.jpg | 1 |
| 1 | http://website.com/img2.jpg | 0 |
| 1 | http://website.com/img3.jpg | 0 |
+-------------+-----------------------------+---------+
mainTableID
是主表的外键,primary
是指示哪一个是"主映像"的位字段。这被称为规范化。
尝试:
第一个$行是一个数组,其中[0]是整行(只有一列)。$picture是第一行的第一部分。
$row2=mysql_fetch_row($result6);
$picture=爆炸(",",$row2[0]);