Welcome Guest, Not a member yet? Register   Sign In
confused on sql and codeigniter
#1

[eluser]Lacsap[/eluser]
Hello Smile
Ive got a Problem with showing my results. Ive write a Query where two tables got a collum with the name "name".
My code :
Controller:
Code:
<?php
$this->db->select('table.name AS thename');
//and more
$query = $this->db->get('mytable');
$data['record'] = $query->result();?>
View:
Code:
<?php if(isset($record)) : foreach($record as $row) : ?>
<td>&lt;?= anchor('mylink'.$row->id,$row->thename) ?&gt;</td>
&lt;?php endforeach; ?&gt;

Error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$thename

Filename: *let us dance* Smile

Line Number: 22

line number 22 is:
Code:
<td>&lt;?= anchor('mylink'.$row->id,$row->thename) ?&gt;</td>
Whats wrong? :-S Thanks Smile
#2

[eluser]bretticus[/eluser]
Yeah, I've noticed this little issue too. I admit that I "just tried something" at the spur of the moment (there may be a more official way.) My solution: add a column alias in my query. You've done that but you might need to tell codeigniter to leave your statement alone by telling it not to insert backticks. You do that by inserting FALSE into the 2nd parameter of select.

->select('table.name AS thename', FALSE)

This is just a hunch, now for the real problem...

You can't use your $record variable that you pass in the way that you think because you are just getting the result for one record. Pass the $query variable instead.

Code:
&lt;?php
$this->db->select('table.name AS thename');
$query = $this->db->get('mytable');
$data['query'] = $query;
//... call your view with $data param ...
?&gt;

Code:
&lt;?php if($query->num_rows() > 0) : foreach($query->result() as $row) : ?&gt;
<td>&lt;?= anchor('mylink'.$row->id,$row->thename) ?&gt;</td>
&lt;?php endforeach; ?&gt;
#3

[eluser]Lacsap[/eluser]
Thanks first. With only
Code:
$this->db->select('table.name AS thename',FALSE);
it works
but with:
Code:
$this->db->select('table.id AS mt_id','table.name AS mt_name','table.user_id AS mt_user_id',FALSE);
it does'nt work :-( only mt_id works.

do i have to write this in another way?
#4

[eluser]bretticus[/eluser]
[quote author="Lacsap" date="1253911227"]
do i have to write this in another way?[/quote]

Code:
//one at a time or...
$this->db->select('table.id AS mt_id',FALSE)
                     ->select('table.name AS mt_name',FALSE)
                     ->select('table.user_id AS mt_user_id',FALSE);

// or one string (you did mulitiple parameters (wrong)

$this->db->select('table.id AS mt_id,table.name AS mt_name,table.user_id AS mt_user_id',FALSE);
#5

[eluser]Lacsap[/eluser]
puh, thanks alot Smile
#6

[eluser]kmil0[/eluser]
try without 'AS'

Code:
$this->db->select('table.name thename');
//and more
$query = $this->db->get('mytable');
$data['record'] = $query->result();?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB