Article T0ZK CodeSOD: All Wrapped Up

CodeSOD: All Wrapped Up

by
Remy Porter
from The Daily WTF on (#T0ZK)

PHP has a lot of warts, and one of those warts is its approach to database interactions. If you want to talk to a MySQL database, every function for doing it is prefixed mysql_, and for say, Postgres, you'd use functions prefixed pg_. On the other hand, it uses an object oriented approach for talking to SQLite3, while Oracle not only has a set of oci_ prefixed functions, but has to add a bunch of important functions that make it just different enough as to be difficult to switch to.

It's a bit of a mess, is the point, and the result is that a lot of developers will write some wrapper functions to make it easier to write generic code that's not tied to a specific database engine. That is, presumably, what Scott's co-worker had in mind when building this library of wrapper functions:

function fetch_row($resource_result){ // array mysql_fetch_row ( resource result) // Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. return mysql_fetch_row($resource_result);}

Yes- the "wrapper" function is just essentially re-naming the function to strip off the mysql prefix. And yes, Scott's co-worker did this for every method that was part of the MySql API.

Only one actually contained any logic:

function fetch_assoc($resource_result){ // array mysql_fetch_assoc ( resource result) // Returns an associative array that corresponds to the fetched row, or FALSE if there are // no more rows. // check to see if we were sent a null resource if($resource_result == null){ throw new Exception('NULL resource_result sent.'); } return mysql_fetch_assoc($resource_result);}

The rest simply contained copy-and-pasted documentation from the PHP.net documentation site:

function fetch_field($resource_result,$field_offset){ // object mysql_fetch_field ( resource result [, int field_offset]) // Returns an object containing field information. // mysql_fetch_field() can be used in order to obtain information about fields in a certain // query result. If the field offset isn't specified, the next field that wasn't yet // retrieved by mysql_fetch_field() is retrieved. return mysql_fetch_field($resource_result,$field_offset);}function fetch_lengths($resource_result){ // array mysql_fetch_lengths ( resource result) // Returns an array that corresponds to the lengths of each field in the last row fetched // by mysql_fetch_row(), or FALSE on error. return mysql_fetch_lengths($resource_result);}function fetch_object($resource_result){ // object mysql_fetch_object ( resource result) // Returns an object with properties that correspond to the fetched row, or FALSE if there // are no more rows. return mysql_fetch_object($resource_result);}function field_flags($resource_result){ // string mysql_field_flags ( resource result, int field_offset) // mysql_field_flags() returns the field flags of the specified field. The flags are // reported as a single word per flag separated by a single space, so that you can split the // returned value using explode(). return mysql_field_flags($resource_result);}function field_len($resource_result,$field_offset){ // int mysql_field_len ( resource result, int field_offset) // mysql_field_len() returns the length of the specified field. return mysql_field_len($resource_result,$field_offset);}function field_name($resource_result,$field_offset){ // string mysql_field_name ( resource result, int field_index) // mysql_field_name() returns the name of the specified field index. result must be a // valid result identifier and field_index is the numerical offset of the field. return mysql_field_name($resource_result,$field_offset);}function field_seek($resource_result,$field_offset){ // int mysql_field_seek ( resource result, int field_offset) // Seeks to the specified field offset. If the next call to mysql_fetch_field() doesn't // include a field offset, the field offset specified in mysql_field_seek() will be returned. return mysql_field_seek($resource_result,$field_offset);}function field_table($resource_result,$field_offset){ // string mysql_field_table ( resource result, int field_offset) // Returns the name of the table that the specifed field is in. return mysql_field_table($resource_result,$field_offset);}function field_type($resource_result,$field_offset){ // string mysql_field_type ( resource result, int field_offset) // mysql_field_type() is similar to the mysql_field_name() function. The arguments are // identical, but the field type is returned instead. The field type will be one of "int", // "real", "string", "blob", and others as detailed in the MySQL documentation. return mysql_field_type($resource_result,$field_offset);}function free_result($resource_result){ // bool mysql_free_result ( resource result) // mysql_free_result() will free all memory associated with the result identifier result. return mysql_free_result($resource_result);}function get_client_info(){ // string mysql_get_client_info ( void ) // mysql_get_client_info() returns a string that represents the client library version. return mysql_get_client_info();}function get_host_info(){ // string mysql_get_host_info ( [resource link_identifier]) // mysql_get_host_info() returns a string describing the type of connection in use for the // connection link_identifier, including the server host name. If link_identifier is omitted, // the last opened connection will be used. return mysql_get_host_into();}function get_proto_info(){ // int mysql_get_proto_info ( [resource link_identifier]) // mysql_get_proto_info() returns the protocol version used by connection link_identifier. // If link_identifier is omitted, the last opened connection will be used. return mysql_get_proto_info();}function get_server_info(){ // string mysql_get_server_info ( [resource link_identifier]) // mysql_get_server_info() returns the server version used by connection link_identifier. If // link_identifier is omitted, the last opened connection will be used. return mysql_get_server_info();}function info(){ // string mysql_info ( [resource link_identifier]) // mysql_info() returns detailed information about the last query using the given // link_identifier. If link_identifier isn't specified, the last opened link is assumed. return mysql_info();}function insert_id(){ // int mysql_insert_id ( [resource link_identifier]) // mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous // INSERT query using the given link_identifier. If link_identifier isn't specified, the // last opened link is assumed. return mysql_insert_id();}function list_dbs(){ // resource mysql_list_dbs ( [resource link_identifier]) // mysql_list_dbs() will return a result pointer containing the databases available from the // current mysql daemon. Use the mysql_tablename() function to traverse this result pointer, // or any function for result tables, such as mysql_fetch_array(). return mysql_list_dbs();}function list_fields($database,$table){ // resource mysql_list_fields ( string database_name, string table_name [, resource link_identifier]) // mysql_list_fields() retrieves information about the given table name. Arguments are the // database and the table name. A result pointer is returned which can be used with // mysql_field_flags(), mysql_field_len(), mysql_field_name(), and mysql_field_type(). return mysql_list_fields($database,$table);}function list_processes(){ // resource mysql_list_processes ( [resource link_identifier]) // mysql_list_processes() returns a result pointer describing the current server threads. return mysql_list_processes();}function list_tables($database){ // resource mysql_list_tables ( string database [, resource link_identifier]) // mysql_list_tables() takes a database name and returns a result pointer much like the // mysql_query() function. Use the mysql_tablename() function to traverse this result // pointer, or any function for result tables, such as mysql_fetch_array(). return mysql_list_tables($database);}function num_fields($resource_result){ // int mysql_num_fields ( resource result) // mysql_num_fields() returns the number of fields in the result set result. return mysql_num_fields($resource_result);}function num_rows($resource_result){ // int mysql_num_rows ( resource result) // mysql_num_rows() returns the number of rows in a result set. This command is only valid // for SELECT statements. To retrieve the number of rows affected by a INSERT, UPDATE or // DELETE query, use mysql_affected_rows(). return mysql_num_rows($resource_result);}function pconnect($server,$user,$password,$client_flags=NULL){ // resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]]) // Returns a positive MySQL persistent link identifier on success, or FALSE on error. return mysql_pconnect($server,$user,$password,$client_flags);}function ping($resoure_link){ // bool mysql_ping ( [resource link_identifier]) // mysql_ping() checks whether or not the connection to the server is working. If it has // gone down, an automatic reconnection is attempted. This function can be used by scripts // that remain idle for a long while, to check whether or not the server has closed the // connection and reconnect if necessary. mysql_ping() returns TRUE if the connection to the // server is working, otherwise FALSE. return mysql_ping($resoure_link);}function result($resource_result,$row=0,$field=0){ // mixed mysql_result ( resource result, int row [, mixed field]) // mysql_result() returns the contents of one cell from a MySQL result set. The field // argument can be the field's offset, or the field's name, or the field's table dot // field name (tablename.fieldname). If the column name has been aliased ('select foo as // bar from...'), use the alias instead of the column name. return @mysql_result($resource_result,$row,$field);}function select_db($database){ // bool mysql_select_db ( string database_name [, resource link_identifier]) // Returns TRUE on success or FALSE on failure. return mysql_select_db($database);}function db_stat(){ // string mysql_stat ( [resource link_identifier]) // mysql_stat() returns the current server status. // Note: mysql_stat() currently only returns status for uptime, threads, queries, // open tables, flush tables and queries per second. For a complete list of other status // variables you have to use the SHOW STATUS SQL command. return mysql_stat();}function tablename($resource_result,$index){ // string mysql_tablename ( resource result, int i) // mysql_tablename() takes a result pointer returned by the mysql_list_tables() function as // well as an integer index and returns the name of a table. The mysql_num_rows() function may // be used to determine the number of tables in the result pointer. Use the mysql_tablename() // function to traverse this result pointer, or any function for result tables, such as // mysql_fetch_array(). return mysql_tablename($resource_result,$index);}function thread_id(){ // int mysql_thread_id ( [resource link_identifier]) // mysql_thread_id() returns the current thread ID. If the connection is lost and you // reconnect with mysql_ping(), the thread ID will change. This means you should not get the // thread ID and store it for later. You should get it when you need it. return mysql_thread_id();}function unbuffered_query($string){ // resource mysql_unbuffered_query ( string query [, resource link_identifier]) // mysql_unbuffered_query() sends a SQL query query to MySQL, without fetching and buffering // the result rows automatically, as mysql_query() does. On the one hand, this saves a // considerable amount of memory with SQL queries that produce large result sets. On the other // hand, you can start working on the result set immediately after the first row has been // retrieved: you don't have to wait until the complete SQL query has been performed. When // using multiple DB-connects, you have to specify the optional parameter link_identifier. return mysql_unbuffered_query($string);}
puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zABDroPs1bk3A
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments