Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
        } 
    }
share|improve this question

2 Answers 2

up vote 2 down vote accepted
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;
}
share|improve this answer
    
Yes, works flawlessly. Thank you, I understand what you did now. –  iBrazilian Jun 7 '13 at 6:18
1  
You're missing a closing parenthesis in the if statement. Overall, perfect answer. –  iBrazilian Jun 7 '13 at 6:25
  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'];
share|improve this answer
    
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 Jun 7 '13 at 5:51
    
@iBrazilian - See my comments regarding using fetchColumn(). –  Aiias Jun 7 '13 at 5:52
    
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 Jun 7 '13 at 5:59
    
@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. –  Aiias Jun 7 '13 at 6:02
    
This is perfect, works great. I appreciate for all the help. –  iBrazilian Jun 7 '13 at 6:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.