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']

Friday, 12 October 2012

Parsing the Search String/Query String (revised)

This version avoids using the eval() function and so is neater and safer.
[Compared with the version given in an earlier note in this blog]

var GLOBAL={}

location.search.replace(
  new RegExp("([^?=&]+)(=([^&]*))?", "g"),
  function($0, $1, $2, $3) {
    GLOBAL[$1]=$3;
  }
);