PHP Obtaining Result Set Metadata

9.3.5 PHP

I n PHP, m et adat a inform at ion is available aft er a successful call t o mysql_query and rem ains accessible up t o t he point at which you call mysql_free_result . To access t he m et adat a, pass t he result set ident ifier ret urned by mysql_query t o t he funct ion t hat ret urns t he inform at ion you want . To get a row or colum n count for a result set , invoke mysql_num_rows or mysql_num_fields . Met adat a inform at ion for a given colum n in a result set is packaged up in a single obj ect . You get t he obj ect by passing t he result set ident ifier and a colum n index t o mysql_fetch_field , t hen access t he various m et adat a at t ribut es as m em bers of t hat obj ect . These m em bers are sum m arized in t he following t able: M e m be r n a m e M e m be r m e a n in g blob 1 if colum n has a BLOB or TEXT t ype, 0 ot herwise max_length Act ual m axim um lengt h of colum n values in result set multiple_key 1 if colum n is part of a non- unique key, 0 ot herwise name Colum n nam e not_null 1 if colum n values cannot be NULL , 0 ot herwise numeric 1 if colum n has a num eric t ype, 0 ot herwise primary_key 1 if colum n is part of a prim ary key, 0 ot herwise table Nam e of t able t he colum n is part of type Colum n t ype nam e unique_key 1 if colum n is part of a unique key, 0 ot herwise unsigned 1 if colum n has t he UNSIGNED at t ribut e, 0 ot herwise zerofill 1 if colum n has t he ZEROFILL at t ribut e, 0 ot herwise The following code shows how t o access and display result set m et adat a: query = SELECT name, foods FROM profile; print Query: query\n; result_id = mysql_query query, conn_id; if result_id die Query failed\n; metadata information becomes available at this point ... is used below because mysql_num_rows and mysql_num_fields print a message if there is no result set under PHP 4, at least nrows = mysql_num_rows result_id; print Number of rows: nrows\n; ncols = mysql_num_fields result_id; print Number of columns: ncols\n; if ncols == 0 print Note: query has no result set\n; for i = 0; i ncols; i++ { col_info = mysql_fetch_field result_id, i; printf --- Column d s ---\n, i, col_info-name; printf blob: s\n, col_info-blob; printf max_length: s\n, col_info-max_length; printf multiple_key: s\n, col_info-multiple_key; printf not_null: s\n, col_info-not_null; printf numeric: s\n, col_info-numeric; printf primary_key: s\n, col_info-primary_key; printf table: s\n, col_info-table; printf type: s\n, col_info-type; printf unique_key: s\n, col_info-unique_key; printf unsigned: s\n, col_info-unsigned; printf zerofill: s\n, col_info-zerofill; } if ncols 0 dispose of result set, if there is one mysql_free_result result_id; The out put from t he program looks like t his: Query: SELECT name, foods FROM profile Number of rows: 10 Number of columns: 2 --- Column 0 name --- blob: 0 max_length: 7 multiple_key: 0 not_null: 1 numeric: 0 primary_key: 0 table: profile type: string unique_key: 0 unsigned: 0 zerofill: 0 --- Column 1 foods --- blob: 0 max_length: 21 multiple_key: 0 not_null: 0 numeric: 0 primary_key: 0 table: profile type: string unique_key: 0 unsigned: 0 zerofill: 0

9.3.6 Python