Welcome Guest, Not a member yet? Register   Sign In
function() + mysql problem
#1

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

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB