Welcome Guest, Not a member yet? Register   Sign In
Getting single DB results
#1

[eluser]rvent[/eluser]
Hello,

I am having this little problem that i cant seem to find a solution to. I am executing a query to my DB to get a single record once i get it it is used by another function to get the list of records who belong to that value.

EX: I have a workOrder 12345 and workOrder 12345 has many messages all of which have an association to their respective workOrder.

Here is my model function:
Code:
function getWOid($woID)
    {
        $sqlGetWo = "SELECT WOrderID
                     FROM WorkOrder
                     WHERE WOrder = ?
                     LIMIT 1";
                    
        $sqlWoID = $this->db->query($sqlGetWo, array($woID));
        return $sqlWoID;
    }

And here is my test controller function:
Code:
function testDB()
    {
        $wId = $this->input->post('WorkOrder');
        $messageID = NULL;
        $woID = $this->SmtJob->getWOid($wId);
        $row = $woID->row();
        $myID = $row->WOrderID;
                                
        $messageData = array('WOMessage' => $this->input->post('Message'),
                             'WOMessageID' => $this->input->post('WOMessageID'),
                             'AuthorID' => $this->input->post('AuthorID'));
        
        $msgData = array('WOMessage' => $this->input->post('Message'),
                             'WOMessageID' => $messageID,
                             'AuthorID' => $this->input->post('Author'),
                             'WOrderID' => $myID);
                            
        echo "$wId <br>";
        echo $messageData['WOMessage'];
        echo $msgData['WOrderID'];
        
    }

ANd certainly this is a no go since i get error about object properties:
Code:
$woID = $this->SmtJob->getWOid($wId);
$row = $woID->row();
$myID = $row->WOrderID;

the docs say that using "query->" i would need to use the http://ellislab.com/codeigniter/user-gui...sults.html but i feel like using a whole block to get 1 result its a little too much..

Is there another way to get a single result other than:
Code:
$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

Thanks..
#2

[eluser]Michael Wales[/eluser]
Code:
$query->row();
#3

[eluser]rvent[/eluser]
Well thats what i had tried before
Code:
$woID = $this->SmtJob->getWOid($wId);
$row = $woID->row();
$myID = $row->WOrderID;

and it didnt work..

Must i use a "foreach"..? I was trying to jsut assign it to variables instead of using a foreach block..

Thanks...
#4

[eluser]Michael Wales[/eluser]
Code:
function testDB() {
  $wId = $this->input->post('WorkOrder');
  $messageID = NULL;
  $woID = $this->SmtJob->getWOid($wId);
  $myID = $woID->WOrderID;
}

Code:
function getWOid($woID) {
  $this->db->select('WOrderID');
  $query = $this->db->get_where('Workorder', array('WOrder'=>$woID), 1, 0);
  if ($query->num_rows() == 1) {
    return $query->row();
  } else {
    return FALSE;
  }
}
#5

[eluser]tobben[/eluser]
$this->db->where('id', 1);
$query = $this->db->get('cms_customers');
$row = $query->row();
echo $row->title;
#6

[eluser]Tom Glover[/eluser]
Even to get a single row i found that I had to place it in a foreach.
#7

[eluser]Michael Wales[/eluser]
Quote:Even to get a single row i found that I had to place it in a foreach.

You shouldn't have to at all. The one thing to watch out for: the row() method returns an object. Most people pass an array to their Views.


Let's use this horrible example below - make sure to read the comments.
Code:
function test() {
  // Return a single post via the row() method
  $data['one_post'] = $this->posts->get(1);
  // Return 2 posts via the result() method
  $data['two_posts'] = $this->posts->get(2);
  // Return 3 posts via the result_array() method
  $data['three_posts'] = $this->posts->get(3);
}

$one_post will be an object, so you will have to reference it's values like:
Code:
echo $one_post->title;

$two_posts will be an object, so:
Code:
foreach ($two_posts as $post) {
  echo $post->title;
}

$three_posts will be an array, so:
Code:
foreach ($three_posts as $post) {
  echo $post['title'];
}
#8

[eluser]rvent[/eluser]
[quote author="Michael Wales" date="1211921295"]
Code:
function testDB() {
  $wId = $this->input->post('WorkOrder');
  $messageID = NULL;
  $woID = $this->SmtJob->getWOid($wId);
  $myID = $woID->WOrderID;
}

Code:
function getWOid($woID) {
  $this->db->select('WOrderID');
  $query = $this->db->get_where('Workorder', array('WOrder'=>$woID), 1, 0);
  if ($query->num_rows() == 1) {
    return $query->row();
  } else {
    return FALSE;
  }
}
[/quote]

I dont see how i couldnt get it to work before, but now it works... Case of a mondays on tuesday..? maybe...

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB