2012-05-16 61 views
-1

我有一个给定的表结构如何从MySQL表中获取图像

 | ID |    IMAGE    | 
    | 01 |   123.jpg    |  
    | 02 | http://localhost/test/logo.jpg |  
    | 03 | http://localhost/test/pic.jpg |  
    | 04 |   image.png    |  

我得到的图像如下图所示

$qry="SELECT IMAGE FROM `table`"; 
$res=mysql_query($qry); 
while($row=mysql_fetch_row($res)){ 
    if($row[0]) 
    { 
     $pic="http://localhost/test/".$row[0]; 
    } 
    else 
    { 
     $pic=""; 
    } 
    echo $pic; 
} 

,但它不是为02,03图像

+0

$ pic的输出值是多少? – Jeremy1026

+0

for 01)http://localhost/test/123.jpg for02)http:// localhost/test/http://localhost/test/logo.jpg就像上面那样 –

+0

如何检查$ row [0 ]'已经以“http://”或“https://”开头,并且只有在完整的URL没有时才会加上? –

回答

-1
正常工作

我认为你改变你的代码从

$pic="http://localhost/test/".$row[0]; 

$pic="http://localhost/test/".$row[1]; 

,因为你的图像列是$行[1]

+0

查询只是抓住'image'列 –

+0

不,它不是。只有一列被查询。这使得'$ row [0]' – Andy

+0

我的坏...我没有正确查询查询 – Arfeen

2

以下内容仅预先考虑完整的URL时给定的值还没有以“http://”或“https ://“:

if($row[0]) 
{ 
    if (stripos($pic, "http://") === false && stripos($pic, "https://") === false) 
     $pic="http://localhost/test/".$row[0]; 
    else 
     $pic=$row[0]; 
} 
else 
{ 
    $pic=""; 
} 
0

您似乎没有一种标准的方式将图像存储在表格中。如果这些是唯一的格式,则在将图像字符串放在一起之前,您需要添加某种检查。

while($row=mysql_fetch_row($res)){ 
if($row[0]) 
    { 
    if 
    $pic = (strpos($row[0], "//") === false) ? "http://localhost/test/".$row[0]: $row[0]; 
    } 
    else 
    { 
    $pic=""; 
    } 
echo $pic; 
} 
-1

显然,你得到这个网址的图片02和03:

http://localhost/test/http://localhost/test/logo.jpg 
http://localhost/test/http://localhost/test/pic.jpg 

这显然是不好的形成。 也许您想将此添加到您的代码中...

if($row[0]) 
    { 
    if (strpos($row[0],'/')) { 
    $pic=$row[0]; 
    } else { 
    $pic="http://localhost/test/".$row[0]; 
    } 
} else ... 
+0

检查“/”是不够的,因为'$ row [0]'可能包含类似'pathcomponent/file。 jpg',它必须有协议部分的前置。 –