CodeIgniter Forums
Troubleshooting my SQL query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Troubleshooting my SQL query (/showthread.php?tid=26733)

Pages: 1 2


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]jonyork[/eluser]
Alright, if I run an sql query in phpmyadmin looking like this I get my desired value

Code:
SELECT `company` FROM `user` WHERE `username` = 'jonathan'

However, ive been hitting my head on the table. this is my CI code

Code:
$this->db->select('company');
$this->db->where('username', 'jonathan');
$company = $this->db->get('user');

and this is the subsequent print_r output

Code:
CI_DB_mysql_result Object ( [conn_id] => Resource id #28 [result_id] => Resource id #33 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 1 [row_data] => )

help is greatly appreciated


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]JHackamack[/eluser]
try
Code:
print_r($company->result());
for the whole output or

Code:
print_r($company->row());

for just the first row of the returned result.


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]ascotan[/eluser]
As noted above you don't want to print the entire CI_DB_mysql_result object structure. Use the Active Record methods to get your results like "row" and "results"


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]bretticus[/eluser]
$this->db->last_query();


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]jonyork[/eluser]
Quote: $this->db->last_query();

Code:
$company = $this->db->last_query();

echo $company;

returns with SELECT `company` FROM (`user`) WHERE `username` = 'jonathan'


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]jonyork[/eluser]
Quote: $this->db->last_query();

Code:
$company = $this->db->last_query();

echo $company;

returns with SELECT `company` FROM (`user`) WHERE `username` = 'jonathan'

Quote: print_r($company->row());

returns with stdClass Object ( [company] => ValueDesired )


Forgive me for being new at CI, but where/what do I do to get it as string variable $company?


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]JHackamack[/eluser]
Code:
$company = $this->db->get('user');
$company = $company->row();

echo $company->company;



Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]jonyork[/eluser]
[quote author="JHackamack" date="1264151862"]
Code:
$company = $this->db->get('user');
$company = $company->row();

echo $company->company;
[/quote]

when I use this, i get the following

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_DB_mysql_result::$company

Filename: views/members_area.php

Line Number: 11

were getting closer (does everyone run into these snags at first or is it just me?)


Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]JHackamack[/eluser]
It took me a bit to get the active record class but I've managed to get it down pat, at least I thought.

Try this:
Code:
$this->db->select('company');
$this->db->where('username', 'jonathan');
$user = $this->db->get('user');
$company = $user->row();
echo $company->company;



Troubleshooting my SQL query - El Forum - 01-21-2010

[eluser]jonyork[/eluser]
Awesome! Thank you for all your help. Might have a few more questions in the next few days, but so far, im really liking CI. Theres a learning curve for sure, but it does seem to help out in the long run.

heres the final code

Code:
<?php

$this->db->select('company')->from('user')->where('username', 'jonathan');
$user = $this->db->get();
$company = $user->row();
            
echo $company->company;

?>