0

I'm currently trying to fetch two images location from my database, how do I return both columns and if both empty then echo another image. This is what I've got so far. How do I return both photo and photo_small so that I may echo them in a php file.

PUBLIC FUNCTION Profile_Pic($uiD) {
    $sth = $this->db->prepare("SELECT photo,photo_small FROM users WHERE uiD = :id");
    $sth->execute(array(':id' => $uiD));

        if ($sth->rowCount() > 0) {
                $data = $row['photo'];
            return $data; 
        } else {
            $data = './icons/users.png';
            return $data;
        } 
    }
0

2 Answers 2

3
PUBLIC FUNCTION Profile_Pic($uiD) {
    $sql = "SELECT photo,photo_small FROM users WHERE uiD = ?";
    $sth = $this->db->prepare($sql);
    $sth->execute(array($uiD));
    $data = $sth->fetch();
    if (empty($data['photo'])) {
        $data['photo'] = './icons/users.png';
    }
    if (empty($data['photo_small'])) {
        $data['photo_small'] = './icons/users.png';
    }
    return $data;
}

if you want to replace both images if even one is absent

PUBLIC FUNCTION Profile_Pic($uiD) {
    $sql = "SELECT photo,photo_small FROM users WHERE uiD = ?";
    $sth = $this->db->prepare($sql);
    $sth->execute(array($uiD));
    $data = $sth->fetch();
    if (empty($data['photo']) || empty($data['photo_small'])) {
        $data['photo'] = './icons/users.png';
        $data['photo_small'] = './icons/users.png';
    }
    return $data;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, works flawlessly. Thank you, I understand what you did now.
You're missing a closing parenthesis in the if statement. Overall, perfect answer.
0
  1. Just return all of the values you want in an array.

  2. You can ensure that both photo and photo_small are not empty strings or NULL by using empty().

  3. Don't forget to retrieve your row using PDOStatement::fetch().

  4. You should not use rowCount() to determine the number of rows returned in a SELECT statement. According to the documentation for PDOStatement::rowCount():

    For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement.

Try this:

$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row && !empty($row['photo']) && !empty($row['photo_small'])) {
  $data = array('photo' => $row['photo'], 'photo_small' => $row['photo_small']);
  return $data; 
} else {
  $data = array('photo' => './icons/users.png', 'photo_small' => './icons/users.png');
  return $data;
} 

Then when you call the function, your returned result can be used like this:

$uiD = 1;
$result = Profile_Pic($uiD);
$photo = $result['photo'];
$photo_small = $result['photo_small'];

4 Comments

This has worked. I don't understand why you received a -1. Why return it in an array instead of just pulling the data?
@iBrazilian - See my comments regarding using fetchColumn().
FetchColumn doesn't seem to be working. I deleted the fetch inside the if parameters as it's already defined outside of it but it doesn't return the database info. Only loads the icons/users.png. Note: I tried both your original and without the fetch inside if.
@iBrazilian - I simplified my answer to simply check against the boolean value of $row. If there was no row returned, this should evalute to FALSE.

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.