CodeIgniter Forums
function() + mysql problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: function() + mysql problem (/thread-34025.html)



function() + mysql problem - El Forum - 09-16-2010

[eluser]brian.89[/eluser]
hy!

I've got a problem:

.../view/message.php :

function users($srt)
{

$query = $this->db->query("select first_name,last_name from bw_users where id='".$str."'");
if ($query->num_rows() > 0)
{
$row=$query->row();
return $row->last_name . $row->first_name;
}
else
{
return "fatal error";
}
}


.....

<?
$x=0;
$query = $this->db->query("select * from bw_message where userid='".$this->session->userdata('userid')."' and label='0' order by date desc");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
?>
<tr>
<td>
&lt;input type="checkbox" name="chkName"&gt;
</td>
<td>
&lt;?= users($row->sent_id); ?&gt;
</td>
<td>
&lt;?= $row->subject; ?&gt;
</td>
<td>
&lt;?= $row->date; ?&gt;
</td>
</tr>
&lt;?
$x++;
}
}
?&gt;


users($row->sent_id); dont work ... not connected to database Sad what is the problem?

when: return "xy"; work... but i have database select error


please help me.


function() + mysql problem - El Forum - 09-16-2010

[eluser]samitrimal[/eluser]
Firstly go to application/config/autoload.php and check whether database class is loaded or not.Add your controller so i can try to help u.


function() + mysql problem - El Forum - 09-16-2010

[eluser]brian.89[/eluser]
solution:

function users($db, $str)
{
$query = $db->db->query("select * from bw_users where id='".$str."'");
if ($query->num_rows() > 0)
{
$row=$query->row();
return $row->username;
}
else
{
return "fatal error";
}
}


....


&lt;?= users($this,$row->sent_id); ?&gt;

$this == database ... i give it to function().

that's work!

thx


function() + mysql problem - El Forum - 09-16-2010

[eluser]danmontgomery[/eluser]
Actual solution: Don't make database calls in views
Technical solution:
Code:
function users($str) {
    $CI =& get_instance();
    $query = $CI->db->query('select * from bw_users where id="'.$str.'"');
    // etc
}



function() + mysql problem - El Forum - 09-16-2010

[eluser]brian.89[/eluser]
thx! Smile