Welcome Guest, Not a member yet? Register   Sign In
Stumped
#1

[eluser]quibstar[/eluser]
I'm trying to retrieve one object from a table, Just the username from a user table using the blog id from a blog table

This is in my model:
Code:
function getUsername($blog_id)
    {
    $q=mysql_query("SELECT username
                   FROM blog
                   JOIN users USING ( user_id )WHERE blog_id=$blog_id");
        $data = $this->db->query($q);
        return $data->result();
    }

This is in my controller:
Code:
$this->load->model('blog_model');
            $username = $this->blog_model->getUsername($this->input->post('id'));

I keep getting errors like:
A PHP Error was encountered

Severity: Warning

Message: preg_match() expects parameter 2 to be string, resource given

Filename: mysql/mysql_driver.php

Line Number: 160
A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #41' at line 1

Resource id #41


Any help would be appreciated. Thanks
#2

[eluser]Colin Williams[/eluser]
You are sending the query() method a mysql_query() result. You just need to send it the string

Code:
function getUsername($blog_id)
    {
    $q="SELECT username
                   FROM blog
                   JOIN users USING ( user_id )WHERE blog_id=$blog_id";
        $data = $this->db->query($q);
        return $data->result();
    }

The whole point of AR is to be driver agnostic. Leave your mysql functions at home
#3

[eluser]quibstar[/eluser]
I did this, but it emailed everyone in the user database.
Code:
function getUsername($blog_id)
    {
    $this->db->select('username');
        $this->db->from('users');
        $this->db->join('blog','users.user_id');
        $this->db->where('blog_id',$blog_id);
        $q = $this->db->get();
        if($q->num_rows() > 0){    
            foreach ($q->result() as $row)
            {
               $data[]= $row->username;
            }
            return $data;
        }
    }
#4

[eluser]Johan André[/eluser]
Code:
function get_username($blog_id)
{
   $this->db->select('users.username');
   $this->db->from('blog');
   $this->db->join('users', 'users.id = blog.user_id');
   $this->db->where('blog.blog_id', $blog_id);

   ...
}




Theme © iAndrew 2016 - Forum software by © MyBB