Welcome Guest, Not a member yet? Register   Sign In
one query's multiple results into new query? :S
#1

[eluser]Maiden13[/eluser]
Hi there,

I have succeeded in querying one table to get information that is needed to query another table (If you can see a better way I would be grateful!)
My question is: How can I have multiple values come back from the first query and have my second query come back with multiple results.
As you can see I am inserting the returned result from query one into query two "msg_id = ?"(I use $datas to fill the '?') but if my results from query one have multiple values then how will this work? Also how can I make it get multiple results from query one? at the moment if there are multiple values in mysql it only grabs the first one it reads.

My MODEL code is as follows:
Code:
function check() {
        $this->db->select('msgto_message');
        $this->db->from('msgto');
        $this->db->where('msgto_display', 'y');
        $this->db->where('msgto_recipient', '1');
        
        $w = $this->db->get();
        
        if ($w->num_rows() > 0) {
               $rowe = $w->row_array();

               $datas = $rowe['msgto_message'];
        }

        $sql = "SELECT msg_content FROM msg WHERE msg_id = ?";

    $data = $this->db->query($sql, $datas) or die(mysql_error());
        
    if ($data->num_rows() > 0) {
        foreach($data->result_array() as $row) {
            $data = $row;
            }
            
            return $data;
        }
        
    }

My CONTROLLER code is as follows:
Code:
function index() {
        $this->load->model('data_model');
        $data['rows'] = $this->data_model->check();
        
        $this->load->view('home', $data);
    }

Thank you anyone that helps me, I greatly appreciate it!
#2

[eluser]WanWizard[/eluser]
$this->db->get returns a database object, not a value. So you can't use it directly in a query string, like you do it now.
#3

[eluser]Maiden13[/eluser]
Would this be better? Im not sure if im adding the var($w) correctly im getting an errors saying it could not be converted to string, so im guessing its meant to be done another way? or am I still dealing with an object?

Thank in advance.

Code:
function check() {

$e = "SELECT msgto_message FROM msgto WHERE msgto_display = 'y' AND msgto_recipient = '1'";
        
$w = $this->db->query($e);

$sql = "SELECT msg_content FROM msg WHERE msg_id = '$w'";
$q = $this->db->query($sql);
        
if($q->num_rows() > 0) {
    foreach ($q->result() as $row) {
        $data[] = $row;
    }
    return $data;
}

}
#4

[eluser]WanWizard[/eluser]
As I said, the result of a database query is a result object. And not a string.

Do a var_dump($w) to check what it is, and maybe refresh your knowledge by reading the database section of the user guide...
#5

[eluser]Maiden13[/eluser]
Thanks very much WanWizard for advise on "var_dump" Smile




Theme © iAndrew 2016 - Forum software by © MyBB