无法从功能获取图像URL

时间:2016-02-22 04:50:32

标签: php image function

使用php Function从sql获取图片网址 但我无法获得图片网址

获取图片网址的功能代码

function getUsers() {//returns an array of users
    global $db;
    $list = array();
    $stmt = $db->prepare('SELECT username FROM user_info');
    $users = $stmt->fetch(PDO::FETCH_ASSOC);
    if (is_array($users) || is_object($users)) {
        foreach ($users as $user) {
            array_push($list, $user[0]);
        }return $list;
    }
}

$users = getUsers();

function getImageURL($user) {
    global $db;
    $stmt = $db->prepare('SELECT propic FROM user_info WHERE username=:username');
    $stmt->execute(array(':username' => $user));
    $source = $stmt->fetch(PDO::FETCH_ASSOC);
    return $source;
}

这是显示图像的代码

<?php
                $user = $_GET['myUser'];
                if (isset($_SESSION['username'])) {
                    $loggedIn = $_SESSION['username'];
                }

                $s = getImageURL($user);
            foreach($s as $URL){
                echo "<img class=\"full\" src=\"$URL[0] \" alt=\"$user's profile picture ERROR PART\"/>";
            }
                ?>

我的数据库连接和用户名一切正常 enter image description here

1 个答案:

答案 0 :(得分:1)

你忘记了两件事:

  • fetchAll()获取数组结果集

    改变这个:

    $source = $stmt->fetch(PDO::FETCH_ASSOC);
    

    到此:

    $source = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
  • 每次迭代获取数组元素的正确方法

    改变这个:

    $URL[0]
    

    到此:

    $URL['propic']