[英]Insert Base64 Images to Array PHP PDO
I'm trying to insert Blob images retrieved from MySQL into an array using a while loop. 我正在尝试使用while循环将从MySQL检索到的Blob图像插入到数组中。
The database statement selected all images then i need to pass them all into an array. 数据库语句选择了所有图像,然后我需要将它们全部传递到数组中。
So in theory, i should have an array of base64 encoded image corresponding to each record from my database. 因此,从理论上讲,我应该有一个与我的数据库中的每个记录相对应的base64编码图像数组。
Any help will be appreciated. 任何帮助将不胜感激。
$sql = "SELECT img from artistlocation";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$data = array();
while($result = $stmt->fetch(PDO::FETCH_OBJ))
{
//$result = base64_encode(); ---- Something here im guessing
$data[] = $result;
}
print_r ($data);
}
catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
I see big issue i your code.. 我看到您的代码有大问题。
You use the same variable, $result
, for fetching mysql row result and image content. 您使用相同的变量$result
来获取mysql行结果和图像内容。
Second, you miss to encoding to base64 the file content. 其次,您错过了将文件内容编码为base64的功能。
Here is my solution: 这是我的解决方案:
$sql = "SELECT img from artistlocation";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$data = array();
while($result = $stmt->fetch(PDO::FETCH_OBJ))
{
$data[] = base64_encode($result['img']);
}
print_r ($data);
}
catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
Hope it will helps you. 希望对您有帮助。
So the answer to this question came from AbraCadaver. 因此,该问题的答案来自AbraCadaver。
$sql = "SELECT img from artistlocation";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$data = array();
while($result = $stmt->fetch(PDO::FETCH_OBJ))
{
$data[] = base64_encode($result->img);
}
Thanks guys for the help! 谢谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.