PHP函数不返回一个foreach中的MySQL查询的所有结果 [英] php function not returning all results from a MySQL query in a foreach
问题描述
这个函数的问题是这样的,在返回数据之后,我只能查看从数据库中检索到的一条记录。可能是简单的东西,但它超越了我。
我想在将它传递给控制器或视图之前执行此操作。也许这是不可能的foreach循环?我错过了什么?
以下是我的代码示例。
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); ($ row-> Thumb_Url == NULL){
$ image $($ data $& = 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;
}
返回$ new_data;
}
希望有人能帮助我呢?谢谢!
现在你只是返回最后一个数据行。像这样改变你的代码,从这个函数返回所有行的数组:
$ p $ $ $ $ $ row = array $ b 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;
}
返回$ rows;
这样,从数据库返回的每一行都会被添加到一个名为 $行
。最后你必须返回你的新数组。
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!
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.
这篇关于PHP函数不返回一个foreach中的MySQL查询的所有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!