0

Am using php Function to get image URL from sql but am unable to get image url

function code to get image url

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;
}

and here is code to display image

<?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\"/>";
            }
                ?>

My database connection and user name everything works fine enter image description here

5
  • what is the output of $URL[0] ? Commented Feb 22, 2016 at 4:55
  • it should be $URL['propic'] Commented Feb 22, 2016 at 4:56
  • its just s value = "s" i have attached image Commented Feb 22, 2016 at 4:56
  • @CodeGodie Doesn't works Commented Feb 22, 2016 at 5:04
  • try this print_r($URL[0]);exit; check whether link is getting or not Commented Feb 22, 2016 at 6:41

1 Answer 1

1

You are forgetting two things:

  • fetchAll() to obtain your array result set

    change this:

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

    to this:

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

  • the correct way to grab your array elements per iteration

    change this:

    $URL[0]
    

    to this:

    $URL['propic']
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.