I'm expecting one row query result in my query commands below:

//Retrieve ID Preview Information fom the database  
$query = db_select('student', 't1',array('target' => 'import'));
$query->join('studinfo', 't2', 't1.studid = t2.studid');
$query->join('semstudent', 't3', 't1.studid = t3.studid');
$query->join('program', 't4', 't4.progcode = t3.studmajor');

$result = $query
   ->fields('t1', array('lastname', 'firstname', 'middlename', 'extname'))
   ->fields('t2', array('guardian', 'mobile', 'homephone'))   
   ->fields('t4', array('progcode', 'college'))    

    foreach($result as $row ){
        watchdog('print', 'inside the loop');
    watchdog('print','not successful');

In my watchdog table in my database, 'successful' is the only log I saw. That means it doesn't get inside my loop which I know for sure that I have one row result. Where am I missing?

  • 1
    If the foreach isn't entered it's more than likely the query doesn't have any results. Check count($result->fetchAll()) to make sure – Clive Apr 22 '15 at 9:01
  • @Clive --> I'm pretty much sure that I must have one row of result. I confirmed it through looking in my database. – user46175 Apr 22 '15 at 9:04
  • 1
    The results suggest otherwise. Check what you've actually got in php, not in the db – Clive Apr 22 '15 at 9:06
  • var_dump($result); or install devel and dpm($result) – Beebee Apr 22 '15 at 9:08
  • After the ->execute(); use a ->fetch of your choice. Dynamic Query result sets – DeveloperWeeks Jul 8 '15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy