PHP Checking for Errors

I nvoke t he trace m et hod wit h an argum ent indicat ing t he t race level. Levels 1 t o 9 enable t racing wit h increasingly m ore verbose out put , and level 0 disables t racing: DBI-trace 1; enable tracing, minimal output DBI-trace 3; elevate trace level DBI-trace 0; disable tracing I ndividual dat abase and st at em ent handles have trace m et hods, t oo. That m eans you can localize t racing t o a single handle if you want . Trace out put norm ally goes t o your t erm inal or, in t he case of a web script , t o t he web servers error log . You can writ e t race out put t o a specific file by providing a second argum ent indicat ing a filenam e: DBI-trace 1, tmptrace.out; I f t he t race file already exist s, t race out put is appended t o t he end; t he files cont ent s are not cleared first . Beware of t urning on a file t race while developing a script , t hen forget t ing t o disable t he t race when you put t he script int o product ion. Youll event ually find t o your chagrin t hat t he t race file has becom e quit e large. Or worse, a filesyst em will fill up and youll have no idea why

2.3.5 PHP

I n PHP, m ost funct ions t hat can succeed or fail indicat e what happened by m eans of t heir ret urn value. You can check t hat value and t ake act ion accordingly. Som e funct ions also print a warning m essage when t hey fail. mysql_connect and mysql_select_db bot h do t his, for exam ple. Aut om at ic print ing of warnings can be useful som et im es, but if t he purpose of your script is t o produce a web page which is likely , you m ay not want PHP t o splat t er t hese m essages int o t he m iddle of t he page. You can suppress such warnings t wo ways. First , t o prevent an individual funct ion call from producing an error m essage, put t he warning- suppression operat or in front of it s nam e. Then t est t he ret urn value and deal wit h errors yourself. That was t he approach used for t he previous sect ion on connect ing t o t he MySQL server, where connect .php print ed it s own m essages: if conn_id = mysql_connect localhost, cbuser, cbpass die Cannot connect to server\n; print Connected\n; if mysql_select_db cookbook, conn_id die Cannot select database\n; Second, you can disable t hese warnings globally by using t he error_reporting funct ion t o set t he PHP error level t o zero: error_reporting 0; However, be aware t hat by t urning off warnings t his way, you wont get any not ificat ion for t hings t hat are wrong wit h your script t hat you really should know about , such as par se er r or s caused by m alform ed synt ax. To obt ain specific error inform at ion about failed MySQL- relat ed operat ions, use mysql_errno and mysql_error , which ret urn a num eric error code and descript ive error st ring. Each funct ion t akes an opt ional connect ion ident ifier argum ent . if you om it t he ident ifier, bot h funct ions assum e you want error inform at ion for t he m ost recent ly opened connect ion. However, prior t o PHP 4.0.6, bot h funct ions require t hat t here is a connect ion. For older versions of PHP, t his requirem ent m akes t he error funct ions useless for report ing problem s wit h t he connect ion-est ablishm ent rout ines. I f mysql_connect or mysql_pconnect fail, mysql_errno and mysql_error ret urn 0 and t he em pt y st ring, j ust as if no error had occurred. To work around t his, you can use t he PHP global variable php_errormsg inst ead, as shown in t he following exam ple. The code shows how t o print error m essages, bot h for failed connect ion at t em pt s and for errors t hat occur subsequent t o a successful connect ion. For problem s connect ing, it at t em pt s t o use mysql_errno and mysql_error if t hey ret urn useful inform at ion. Ot herwise, it falls back t o using php_errormsg : if conn_id = mysql_connect localhost, cbuser, cbpass { If mysql_errno mysql_error work for failed connections, use them invoke with no argument. Otherwise, use php_errormsg. if mysql_errno { die sprintf Cannot connect to server: s d\n, htmlspecialchars mysql_error , mysql_errno ; } else { die Cannot connect to server: . htmlspecialchars php_errormsg . \n; } } print Connected\n; if mysql_select_db cookbook, conn_id { die sprintf Cannot select database: s d\n, htmlspecialchars mysql_error conn_id, mysql_errno conn_id; } The htmlspecialchars funct ion escapes t he , , and charact ers so t hey display properly in web pages. I t s useful here when displaying error m essages because we dont know what part icular charact ers a m essage cont ains. Use of php_errormsg requires t he track_errors variable t o be enabled in your PHP init ializat ion file. On m y syst em , t hat file is usr local lib php.ini. Locat e t he file on your syst em , t hen m ake sure t he track_errors line looks like t his: track_errors = On; I f you change t he track_errors set t ing and youre using PHP as an Apache m odule, youll need t o rest art Apache t o m ake t he change t ake effect .

2.3.6 Python