populating an associative array from an sql return stryct |
Trying to move data from mysql over to a structure.
$data['email'] = $row->email; $data['year'] = $row->year; But when I look at $data, the fields are null even though the $row is populated. What is wrong with this? I need to get the data into an associative array.
proof that an old dog can learn new tricks
I changed it to this:
$data = array( 'email' => $row->email, 'year' => $row->year, 'campaign' => $row->campaign, 'data_time' => uniqid(), 'hours' => $row->hours, 'week' => $row->week, 'business_component' => $row->business_comp, 'project' => $row->project, 'activity' => $row->activity); But it is still not working. The elements of $data are still null.
proof that an old dog can learn new tricks
Place these two methods in a helper file and load it.
PHP Code: // ----------------------------------------------------------------------- These two methods will convert (array to object and object to array). What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I create MY_array_helper and put it in the helpers directory. I load it with:
$this->load->helper('string','MY_array_helper'); And then I try calling it with $data=objectToArray($result); The $result struct is from my call to mySQL Query(). I get Message: Call to undefined function objectToArray(). Is the result from the mysql query an object? Perhaps I want to do $data2=objectToArray($result) instead, and then have: $data = array( 'email' => $data2->email, 'year' => $data2->year, 'campaign' => $data2->campaign, 'data_time' => uniqid(), 'hours' => $data2->hours, 'week' => $data2->week, 'business_component' => $data2->business_comp, 'project' => $data2->project, 'activity' => $data2->activity);
proof that an old dog can learn new tricks
Got it. This is the solution:
$data = array( 'email' => $row['email'], 'year' => $row['year'], 'campaign' => $row['campaign'], 'data_time' => uniqid(), 'hours' => $row['hours'], 'week' => $row['week'], 'business_component' => $row['business_comp'], 'project' => $row['project'], 'activity' => $row['activity']);
proof that an old dog can learn new tricks
Not sure where you got $result from but it looks like you should have passed $row to the method.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(08-26-2018, 04:28 AM)InsiteFX Wrote: Not sure where you got $result from but it looks like you should have passed $row to the method. https://www.codeigniter.com/user_guide/d...sults.html
proof that an old dog can learn new tricks
I know all about those results.
In your first post you mention $row but then you are passing $result to the $data = objectToArray($result); This should be passed the result from the database. $data = objectToArray($row); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |