Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:13:14+00:00 2026-06-17T21:13:14+00:00

Hey guys I have a little issue with a function that retrieves data from

  • 0

Hey guys I have a little issue with a function that retrieves data from a MySQL Database and then I iterate over the results with a foreach loop, checking a value to see if it is null and if it is, replacing it with another value.

The problem with this function is this, that after returning the data I’m only able to view one record retrieved from the database. Probably something simple but it’s beyond me.

I would like to do this before passing it to the controller or view. Maybe this isn’t possible with the foreach loop? What am I missing?

Here is an example of my code.

public function get_basic_user_data(){
    $sql = 'SELECT Account.First_Name, Account.Last_Name, Account.User_Name, Profile_Photos.Thumb_Url 
            FROM Account 
            LEFT JOIN Profile_Photos ON Account.idAccount = Profile_Photos.Account_Id 
            AND Profile_Photos.Active = 1
            WHERE Account.idAccount != ?';
    $account_id = $this->get_account_id();
    $data = $this->db->query($sql, $account_id);

    foreach($data->result() as $row){

            if($row->Thumb_Url == NULL){
                $image = base_url().'assets/images/no_photo_thumb.png';
            }else{
                $image = $row->Thumb_Url; 
            }


    $new_data = new stdClass;
    $new_data->First_Name = $row->First_Name;
    $new_data->Last_Name = $row->Last_Name;
    $new_data->User_Name = $row->User_Name;
    $new_data->Thumb_Url = $image;

    }   

    return $new_data;

}   

Hopefully someone can help me with this? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:13:15+00:00Added an answer on June 17, 2026 at 9:13 pm

    At the moment you are just returning the last data row. Change your code like this to return an array of all your rows from that function:

    $rows = array()
    foreach($data->result() as $row){
    
        if($row->Thumb_Url == NULL){
            $image = base_url().'assets/images/no_photo_thumb.png';
        }else{
            $image = $row->Thumb_Url; 
        }
    
    
        $new_data = new stdClass;
        $new_data->First_Name = $row->First_Name;
        $new_data->Last_Name = $row->Last_Name;
        $new_data->User_Name = $row->User_Name;
        $new_data->Thumb_Url = $image;
    
        $rows[] = $new_data;
    }   
    
    return $rows;
    

    This way every row returned from the database will be added to an array named $rows. At the end you have to return your new array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I have an ajax jquery function that receives data from a php
Hey guys I have a little issue here. I have a panel where I
Hey guys I have an assigned array from mysql results and I simply want
Hey guys I have a query that selects data and organizes but not in
Hey guys, I have a little issue here. I need my users to be
Hey guys I have a problem with MySQL, i'm trying to insert data in
Hey guys I have a problem with MySQL, i'm trying to insert data in
Hey guys, have a little issue i need to figure out. I created a
Hey, guys. I work for http://pastebin.com and we have a little issue with the
Hey guys, I have a little button functionality that is not erring, but also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.