get_magic_quotes_gpc(), or not

I would like to sanitize user-supplied HTTP request variables before sending them to a database. There is a deprecated PHP setting called magic_quotes_gpc that automatically escapes GET, POST, and COOKIE (GPC) operators. Please understand that magic_quotes_gpc is highly discouraged. <?php // in a world with magic_quotes_gpc $lastname = get_magic_quotes_gpc() ? stripslashes($_GET[‘lastname’]) : $_GET[‘lastname’]; // if […]

last_insert_id()

I would like to retrieve the id of a row I just inserted. In Oracle this problem is handled with the use of sequences, SQL> SELECT uid_seq.NEXTVAL INTO user_id FROM DUAL; SQL> INSERT INTO users (id, username) VALUES (user_id, ‘joebob’); 1 row created. SQL> — do something else with user_id Basically, a sequence can provide […]

php DB adapter, with magic

I would like to encapsulate the handling of database connections, and maintain a loose coupling between database connections and application and data-access code. I’ll not focus on connection pooling since that can be handled at the driver level independent of the application and adapter code. I would like to leverage the PDO interface as well […]

mysql \c

The mysql command line interface provides several very useful shortcuts. For example, the ‘tee’ command will append everything to a specified outfile: mysql> \T logfile.txt Logging to file ‘logfile.txt’ The ‘clear’ command saves from accidentally hitting Ctrl-C and disconnecting from the database: mysql> select * from typo \c mysql> The ‘clear’ command is especially useful […]

php Namespaces and Class loading

I want to autoload php classes and functions. Ideally, I want a lazy loader (include class files only when they are needed, never including more files than necessary). I’d also like namespace protection (if running PHP 5.3 or later). And of course I want to do this easily without having to keep track of hierarchical […]

python Fibonacci list

The Fibonacci sequence in python looks something like this def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) But expect stack overflows for even small values of n, e.g., fib(60) will require over 3 trillion function calls. You can use the Fibonacci sequence itself to […]

scraping and parsing html

I would like to read J. Krishnamurti books on my Kindle. Unfortunately, no ebooks were available although I did find that jkrishnamurti.org has an extensive collection of books on their website. At present there is no full download, only a per-chapter html viewer, and some of the books ran over 80 chapters, which is more […]

smiley shell prompt, or frown

I’ve been using the following command prompt for years server ~/dir :) whoami victor server ~/dir :) badcommand -bash: badcommand: command not found server ~/dir :( echo sorry sorry server ~/dir :) It’s a smiley emoticon, but only when the previous command returned without error. If the exit code (stored in $?) returns any kind […]

recursive grep

I am trying to search through all files in a directory tree, perhaps to find a specific function declaration. Ideally I want the exact files and line numbers of every search match. A modern GNU grep contains a “-r” recursive options, so for example to find the function declaration (with line number) for “authenticate” you […]