Problem Solution Discussion Perl PHP

The script s cont aining t he code for t he exam ples shown here are in t he m et adat a direct ory of t he recipes dist ribut ion. Som e of t hem use ut ilit y funct ions locat ed in t he lib direct ory. To creat e any t ables t hat you need for t rying t he exam ples, look in t he t ables direct ory. I n several cases, recipes developed here const ruct queries using a dat abase, t able, or colum n nam e t hat is st ored in a variable. For sim plicit y, generally such nam es are insert ed as is int o t he query st ring. For exam ple: query = SHOW COLUMNS FROM tbl_name; This works properly in t he m aj orit y of cases, but t here are som e possible com plicat ions you should know about , and m ay wish t o t ake int o account when adapt ing t hese recipes for your own use. As of MySQL 3.23.6, nam es are allowed t o cont ain alm ost any charact er, such as spaces. I f you ant icipat e a need t o deal wit h such nam es, surround t he nam e wit h backt icks: query = SHOW COLUMNS FROM `tbl_name`; I f t he server is running in ANSI m ode, nam e quot ing should be done wit h double quot es inst ead: query = SHOW COLUMNS FROM \tbl_name\; To deal wit h t hese issues on a general basis, you can query t he server t o see if it is Version 3.23.6 or lat er see Recipe 9.14 , and you can also use SHOW VARIABLES t o see if it is running in ANSI m ode. The recipes here do not perform all t hese checks, because doing so would obscure t heir m ain point .

9.2 Obtaining the Number of Rows Affected by a Query

9.2.1 Problem

You want t o know how m any rows were changed by a query.

9.2.2 Solution

Som e API s provide t he count as t he ret urn value of t he funct ion t hat issues t he query. Ot hers have a separat e funct ion t hat you call aft er issuing t he query.

9.2.3 Discussion

For queries t hat affect rows UPDATE , DELETE , INSERT , REPLACE , each API provides a way t o det erm ine t he num ber of rows involved. For MySQL, affect ed by norm ally m eans changed by, so rows t hat are not changed by a query are not count ed, even if t hey m at ch t he condit ions specified in t he query. For exam ple, t he following UPDATE st at em ent would result in an affect ed by value of zero because it does not change any colum ns from t heir current values, no m at t er how m any rows t he WHERE clause m at ches: UPDATE limbs SET arms = 0 WHERE arms = 0;

9.2.4 Perl

I n DBI script s, t he affect ed- rows count is ret urned by do or by execute , depending on how you execut e t he query: execute query using do my count = dbh-do query; report 0 rows if an error occurred printf d rows were affected\n, defined count ? count : 0; execute query using prepare plus execute my sth = dbh-prepare query; my count = sth-execute ; printf d rows were affected\n, defined count ? count : 0; When you use DBI , you have t he opt ion of asking MySQL t o ret urn t he m at ched by value rat her t han t he affect ed by value. To do t his, specify mysql_client_found_rows=1 in t he opt ions part of t he dat a source nam e argum ent of t he connect call w hen you connect t o t he MySQL server. Heres an exam ple: my dsn = DBI:mysql:cookbook:localhost;mysql_client_found_rows=1; my dbh = DBI-connect dsn, cbuser, cbpass, { PrintError = 0, RaiseError = 1 }; mysql_client_found_rows changes t he row- report ing behavior for t he durat ion of t he connect ion.

9.2.5 PHP

I n PHP, invoke t he mysql_affected_rows funct ion t o find out how m any rows a query changed: result_id = mysql_query query, conn_id; report 0 rows if the query failed count = result_id ? mysql_affected_rows conn_id : 0; print count rows were affected\n; The argum ent t o mysql_affected_rows is a connect ion ident ifier. I f you om it t he argum ent , t he current connect ion is used.

9.2.6 Python