Tuesday, 23 October 2012

EXTRACT-ing results from Pear DB into separate variables


I was used to writing this sort of code when using php_mysql

$result = $db->getRow($sql);
list($surname,$initials,$login,$email,$tutor,$prog)=$result;
//now got separate variables $surname, $initials, etc

Now that I am using PEAR DB, list(...) does not work, so I have found another 'trick' using the PHP function extract

extract($result,EXTR_PREFIX_ALL, "student");
//student is used as a prefix
//now got variables like $student_surname, $student_initials, etc

Note:
I am using 
$db->setFetchMode(DB_FETCHMODE_ASSOC);
so the alternative is to access the names as
$result['surname'], $result['initials']

No comments:

Post a Comment