PHP Connecting to the MySQL Server, Selecting a Database, and Disconnecting

Assum ing t hat connect succeeds, it ret urns a dat abase handle t hat cont ains inform at ion about t he st at e of t he connect ion. I n DBI parlance, references t o obj ect s are called handles. Lat er well see ot her handles, such as st at em ent handles t hat are associat ed wit h part icular queries. DBI script s in t his book convent ionally use dbh and sth t o signify dat abase and st at em ent handles. 2 .2 .4 .1 Addit ion a l con n e ct ion pa r a m e t e r s For connect ions t o localhost , you can provide a mysql_socket opt ion in t he DSN t o specify t he pat h t o t he Unix dom ain socket : my dsn = DBI:mysql:host=localhost;mysql_socket=vartmpmysql.sock . ;database=cookbook; The mysql_socket opt ion is available as of MySQL 3.21.15. For non- localhost connect ions, you can provide a port opt ion t o specify t he port num ber: my dsn = DBI:mysql:host=mysql.snake.net;port=3307;database=cookbook;

2.2.5 PHP

To writ e PHP script s t hat use MySQL, your PHP int erpret er m ust have MySQL support com piled in. I f it doesnt , your script s will t erm inat e wit h an error m essage like t his: Fatal error: Call to undefined function: mysql_connect Should t hat occur, check t he inst ruct ions included wit h your PHP dist ribut ion t o see how t o enable MySQL support . PHP script s usually are writ t en for use wit h a web server. I ll assum e t hat if youre going t o use PHP t hat way here, you can sim ply drop PHP script s int o your servers docum ent t ree, request t hem from your browser, and t hey will execut e. For exam ple, if you run Apache as t he web server on t he host ht t p: apache.snake.net and you inst all a PHP script m yscript .php at t he t op level of t he Apache docum ent t ree, you should be able t o access t he script by request ing t his URL: ht t p: apache.snake.net m yscript .php This book uses t he .php ext ension suffix for PHP script filenam es. I f you use a different ext ension, such as .php3 or .pht m l, youll need t o change t he script nam es or else reconfigure your web server t o recognize t he .php ext ension. Ot herwise, when you request a PHP script from your browser, t he lit eral t ext of t he script will appear in your browser window. You dont want t his t o happen, part icularly if t he script cont ains t he usernam e and password you use for connect ing t o MySQL. For addit ional inform at ion about configuring Apache for use wit h PHP, see Recipe 16.3 . PHP script s oft en are writ t en as a m ixt ure of HTML and PHP code, wit h t he PHP code em bedded bet ween t he special ?php and ? t ags. Here is a sim ple exam ple: html headtitleA simple pagetitlehead body p ?php print I am PHP code, hear me roar\n; ? p body html For brevit y, when I show PHP exam ples consist ing ent irely of code, t ypically I ll om it t he enclosing ?php and ? t ags. Exam ples t hat swit ch bet ween HTML and PHP code include t he t ags. To use MySQL in a PHP script , you connect t o t he MySQL server and select a dat abase in t wo st eps, by calling t he mysql_connect and mysql_select_db funct ions. Our first PHP script , connect .php, shows how t his works: connect.php - connect to the MySQL server 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; mysql_close conn_id; print Disconnected\n; mysql_connect t akes t hree argum ent s: t he host where t he MySQL server is running, and t he nam e and password of t he MySQL account you want t o use. I f t he connect ion at t em pt succeeds, mysql_connect ret urns a connect ion ident ifier t hat can be passed t o ot her MySQL- relat ed funct ions lat er. PHP script s in t his book convent ionally use conn_id t o signify connect ion ident ifiers. I f t he connect ion at t em pt fails, mysql_connect print s a warning and ret urns FALSE . The script prevent s any such warning by put t ing t he warning-suppression operat or in front of t he funct ion nam e so it can print it s own m essage inst ead. mysql_select_db t akes t he dat abase nam e and an opt ional connect ion ident ifier as argum ent s. I f you om it t he second argum ent , t he funct ion assum es it should use t he current connect ion t hat is, t he one m ost recent ly opened . The script j ust shown calls mysql_select_db im m ediat ely aft er it connect s, so t he following calls are equivalent : if mysql_select_db cookbook, conn_id die Cannot select database\n; if mysql_select_db cookbook die Cannot select database\n; I f mysql_select_db select s t he dat abase successfully, it ret urns TRUE . Ot herwise, it print s a warning and ret urns FALSE . Again, as wit h t he mysql_connect call, t he script uses t he operat or t o suppress t he warning. I f you dont want t o select any dat abase, j ust om it t he call t o mysql_select_db . To t ry t he connect .php script , copy it t o your web servers docum ent t ree and request it from your browser. Alt ernat ively, if you have a st andalone version of t he PHP int erpret er t hat can be run from t he com m and line, you can t ry t he script wit hout a web server or browser: php -q connect.php Connected Disconnected PHP act ually provides t wo funct ions for connect ing t o t he MySQL server. The script connect .php uses mysql_connect , but you can use mysql_pconnect inst ead if you want t o est ablish a persist ent connect ion t hat doesnt close when t he script t erm inat es. This allows t he connect ion t o be reused by subsequent PHP script s run by t he web server, t hus avoiding t he overhead of set t ing up a new connect ion. However, MySQL is so efficient at opening connect ions t hat you m ight not not ice m uch difference bet ween t he t wo funct ions. Also, you should consider t hat use of mysql_pconnect som et im es result s in t oo m any connect ions being left open. A sym pt om of t his is t hat t he MySQL server st ops accept ing new connect ions because so m any persist ent connect ions have been opened by w eb server processes. Using mysql_connect rat her t han mysql_pconnect m ay help t o avoid t his problem . 2 .2 .5 .1 Addit ion a l con n e ct ion pa r a m e t e r s For connect ions t o localhost , you can specify a pat hnam e for t he Unix dom ain socket by adding : pathtosocket t o t he host nam e in t he connect call: hostname = localhost:vartmpmysql.sock; if conn_id = mysql_connect hostname, cbuser, cbpass die Cannot connect to server\n; For non- localhost , connect ions, you can specify a port num ber by adding : port_num t o t he host nam e: hostname = mysql.snake.net:3307; if conn_id = mysql_connect hostname, cbuser, cbpass die Cannot connect to server\n; The socket pat hnam e opt ion is available as of PHP 3.0.B4. The port num ber opt ion is available as of PHP 3.0.10. I n PHP 4, you can use t he PHP init ializat ion file t o specify a default host nam e, usernam e, password, socket pat h, or port num ber by set t ing t he values of t he mysql.default_host , mysql.default_user , mysql.default_password , mysql.default_socket , or mysql.default_port configurat ion direct ives.

2.2.6 Python