• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubleshooting my SQL query

#1
[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

#2
[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.

#3
[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"

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

#5
[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'

#6
[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?

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

echo $company->company;

#8
[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?)

#9
[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;

#10
[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;

?>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.