¼o½e
ÔÛÒ»ÆðÀ´¿´¿´PDOÀa¡£PDOÊÇPHP Data ObjectsµÄËoд£¬Ëu±»ÃeÊoΪ¡°ÔÚPHPÖÐÃÎÊÊý¾Ý¿aµÄÇaÁ¿¼¶£¬¼aeÈÝÐԵĽӿڡ±¡£¾¡¹ÜËuµÄÃu×Ö²»Õ¦ºÃÌý£¬µ«PDOÊÇÒ»¸oÔÚPHPÖÐÃÎÊÊý¾Ý¿a»aÈÃÈËϲ°®µÄ*½Ê½¡£
ÓeMySQLiµÄ²»Í¬
MySQLiºÍPDOºÜÏaËÆ£¬¶¼ÓÐÁ½*½ÃaeÖ÷ÒªÇø±ð£º
1.MySQLiÖ»ÄÜÃÎÊMySQL£¬µ«PDO¿ÉÒÔÃÎÊ12ÖÖ²»Í¬µÄÊý¾Ý¿a
2.PDOûÓÐÆÕͨº¯Êýµ÷ÓÃ(mysqli_*functions)
¿ªÊ¼²½Öe
Ê×ÏÈ£¬ÄaµÃȶ¨Ò»ÏÂÄaµÄPHPÊÇñ°²×°ÁËPDO²a¼þ¡£Äa¿ÉÒÔÓÃ$test=new PDO()µÄ½a¹uÀ´²aÊÔ¡£Èç¹uÌaʾ˵ÊDzÎÊý²»Æ¥Åa£¬ÄÇÖ¤Ã÷ÒѾ°²×°ÁËPDO²a¼þ£¬Èç¹u˵ÊǶÔÏo²»´aeÔÚ£¬ÄaµÃÏÈÈÈÏÒ»ÏÂÔÚpho.iniÖÐphp_pdo_yourssqlserverhere.extisÊÇñ±»×¢Ê͵oÁË¡£Èç¹uûÓÐÕa¾a»°£¬ÄÇÄaµÃ°²×°PDOÁË£¬ÕaÀi¾Í²»†ªaÂÁË¡£
Á¬½Ó
ÏÖÔÚÎÒÃÇÈÈÏþÎñÆ÷ÒѾ¹¤×÷£¬¿ªÊ¼Á¬½ÓÊý¾Ý¿a°É£º
$dsn = 'mysql:dbname=demo;host=localhost;port=3306';
$username = 'root';
$password = 'password_here';
try {
$db = new PDO($dsn, $username, $password); // also allows an extra parameter of configuration
} catch(PDOException $e) {
die('Could not connect to the database:<br/>' . $e);
}
³ý$dsnÖ®Ía£¬ËuÓÐÓi¾aºÍ±aÁ¿¶¼ÄÜ×ÔÎÒ½aÊÍ¡£DSNÖ¸µÄÊÇÊý¾ÝÔ´Ãu³Æ£¬ÓжaÖÖÊaÈeÀaÐÍ¡£×i³£¼uµÄÒ»ÖÖÊÇÎÒÃǸոÕÓõÄÕaÖÖ£¬PHP¹ÙÍø½aÊÍÁË ÆaËu¿ÉÓõÄDSN ¡£
Äa¿ÉÒÔÊ¡È¥DSNµÄÆaËu¶iÍa²ÎÊý£¬Ö»ÐeÒªÔÚÊý¾Ý¿aÇý¶¯ºoÃae´ø¸oðºÅ£¬±ÈÈ磨mysql:£(C)¡£ÔÚÕaÖÖÇe¿oÏÂPDO½«»a³¢ÊÔÁ¬½Óµ½±¾µØÊý¾Ý¿a¡£¾ÍÏñµ±ÄaʹÓÃMySQLiʱÐeÒªÔÚ²eѯÖÐÖ¸¶¨Êý¾Ý¿aÃu³ÆÒ»Ñu¡£
×iºoÒ»¼þÄaÐeҪעÒaµÄÊÂÇe¾ÍÊÇÎÒÃÇÓÃtry-catch¿e°u¹uÁËÎÒÃǵijoʼ»¯¶ÔÏo¡£PDOÁ¬½Óʧ°ÜµÄʱºo½«»aÅ׳oPDOExceptionÒi³£¶ø²»ÊDzeѯʧ°ÜµÄʱºo¡£Èç¹uÄaÔ¸ÒaÄa¿ÉÒÔʹÓÃÏÂÃae´uÂe$db=lineÀ´Ñ¡ÔñÒi³£µÄģʽ¡£
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
»oÕßÄa¿ÉÒÔÖ±½ÓÔÚPDO³oʼ»¯Ê±´«µÝ²ÎÊý£º
$db = new PDO($dsn, $username, $password, array (
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
ÎÒÃÇÏÖÔÚʹÓõÄÊÇ´iÎoµÄ½Ê½¡ª¡ªÔÚʧ°Üʱºo¼oµ¥µ»Øfalse,ÎÒÃÇÊÇûÓÐÀiÓɲ»¶ÔÒi³£½øÐд¦ÀiµÄ¡£
»u±¾²eѯ
ÔÚPDOÖÐʹÓÃqueryºÍexecÁ½ÖÖ½¨Ê¹µÃ¶ÔÊý¾Ý¿a²eѯ±aµÃdz£¼oµ¥¡£Èç¹uÄaÏeµÃµ½²eѯ½a¹uµÄÐÐÊýexecÊÇdz£ºÃÓõģ¬Òo´Ë¶ÔSELECT²eѯÓi¾aÊÇ*dz£ÓÐÓõġ£
ÏÖÔÚÎÒÃÇͨ¹ýÏÂÃaeµÄÒ»¸oÀý×Ó²e¿´ÕaÁ½ÖÖ½¨£º
$statement = <<<SQL
SELECT *
FROM `foods`
WHERE `healthy` = 0
SQL;
$foods = $db->query($statement);
¼ÙÉeÎÒÃǵIJeѯÊÇÕýÈ*µÄ£¬$foodsÏÖÔÚÊÇÒ»¸oPDO Statement¶ÔÏo£¬ÎÒÃÇ¿ÉÒÔÓÃËu»ñÈ¡µ½ÎÒÃǵĽa¹u»oÕßÊDze¿´Õa´Î²eѯÖÐÒ»¹²²eµ½¶aÉÙÌo½a¹u¼¯¡£
ÐÐÊý
²»×aµÄÊÇ£¬PDOûÓÐÌa¹(C)Ò»¸oͳһµÄ½¨È¥¼ÆËaµ»ØµÄÐÐÊý¡£PDO Statement°uº¬ÁËÒ»¸o½Ð×orowCountµÄ½¨£¬µ«ÊÇÕa¸o½*¨È´²»Äܱ£Ö¤ÔÚÿһ¸oSQLÇý¶¯ÖÐÆð×÷Óã¨ÐÒÔ˵ÄÊÇ£¬ËuÄܹ»ÔÚMysqlÊý¾Ý¿aÖÐÆð×÷Óã(C)¡£
Èç¹uÄaµÄSQLÇý¶¯²»Ö§³ÖÕa¸o½¨£¬ÄaÒ²ÓÐ2¸oÑ¡Ôñ:ʹÓöþ´Î²eѯ£¨SELECT COUNT(*)£(C)»oÕßʹÓüoµ¥µÄcount£¨$foods£(C)µÃµ½ÐÐÊý¡£
ÐÒÔ˵ÄÊǶÔÎÒÃǵÄMySQLÀý×Ó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÏÂÃaeµÄ¼oµ¥½¨À´Êa³oÕýÈ*µÄÐÐÊý¡£
echo $foods->rowCount();
±eÀu½a¹u¼¯
´oÓ¡³oÕaÐ(C)¿É¿ÚµÄʳÎiÒ»µaÒ²²»À§ÄÑ£º
foreach($foods->FetchAll() as $food) {
echo $food['name'] . '<br />';
}
ΨһÐeҪעÒaµÄÊÇPDOÒ²Ö§³ÖFetch½¨£¬Õa¸o½¨Ö»»aµ»ØµÚÒ»Ìo½a¹u£¬Õa¶ÔÓÚÖ»ÐeÒª²eѯһÌo½a¹u¼¯ÊÇdz£ÓÐÓõġ£
*תÒaÓû§ÊaÈe£¨µÄÌØÊa×Öu£(C)**
Äa¿ÉÔøÌý˵¹ý£¨mysqli_£(C)real_escape_string,ÕaÊÇÓÃÓÚȱ£Óû§ÊaÈe°²È«Êý¾Ý¡£PDOÌa¹(C)ÁËÒ»¸o½¨½Ð×oquote£¬Õa¸o½¨¿ÉÒÔ°ÑÊaÈe×Öu´®ÖдøÓÐÒýºÅµÄµØ½½øÐÐÌØÊa×ÖuתÒa¡£
$input: this is's' a '''pretty dange'rous str'ing
ÔÚתÒaºo£¬×iÖյõ½ÏÂÃae½a¹u£º
$db->quote($input): 'this is\'s\' a \'\'\'pretty dange\'rous str\'ing'
exec()
ÕýÈçÉÏÃaeÌaµ½µÄ£¬Äa¿ÉÒÔʹÓÃexec()½¨ÊµÏÖUPDATE,DELETEºÍINSERT ²Ù×÷£¬Ö´ÐкoËu»a*µ»ØÊÜÓ°ÏiÐÐÊýµÄÊýÁ¿£º
$statement = <<<SQL
DELETE FROM `foods`
WHERE `healthy` = 1;
SQL;
echo $db->exec($statement); // outputs number of deleted rows
Ô¤´¦ÀiÓi¾a
¾¡¹Üexec½¨ºÍ²eѯÔÚPHPÖÐÈÔÈ»±»´oÁ¿Ê¹ÓúÍÖ§³Ö£¬µ«ÊÇPHP¹ÙÍøÉÏ»¹ÊÇÒªÇo´o¼ÒÓÃÔ¤´¦ÀiÓi¾aµÄ*½Ê½À´Ìae´u¡£ÎªÊ²Ã´ÄØ£¿Ö÷ÒªÊÇÒoΪ£ºËu¸u°²È«¡£Ô¤´¦ÀiÓi¾a²»»aÖ±½ÓÔÚʵ¼Ê²eѯÖвaÈe²ÎÊý£¬Õa¾Í±ÜÃaÁËÐi¶aDZÔÚµÄSQL×¢Èe¡£
È»¶ø³oÓÚijÖÖÔÒo£¬PDOʵ¼ÊÉϲ¢Ã»ÓÐÕaeÕýµÄʹÓÃÔ¤´¦Ài£¬ËuÊÇÔÚÄ£ÄaÔ¤´¦Ài½Ê½£¬ÔÚ½«Ói¾a´«¸øSQLþÎñÆ÷֮ǰ»a°Ñ²ÎÊýÊý¾Ý²aÈeµ½Ói¾aÖУ¬ÕaʹµÃijÐ(C)ϵͳÈÝÒ×Êܵ½SQL×¢Èe¡£
Èç¹uÄaµÄSQLþÎñÆ÷²»ÕaeÕýµÄÖ§³ÖÔ¤´¦Ài,ÎÒÃÇ¿ÉÒÔºÜÈÝÒ×µÄͨ¹ýÈçϽʽÔÚPDO³oʼ»¯Ê±´«²ÎÀ´ÐÞ¸´Õa¸oÎÊÌa:
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
½ÓÏÂÀ´¿ªÊ¼ÎÒÃǵĵÚÒ»¸oÔ¤´¦ÀiÓi¾a°É£º
$statement = $db->prepare('SELECT * FROM foods WHERE `name`=? AND `healthy`=?');
$statement2 = $db->prepare('SELECT * FROM foods WHERE `name`=:name AND `healthy`=:healthy)';
ÕýÈçÄaËu¼u£¬ÓÐÁ½ÖÖ´´½¨²ÎÊýµÄ½¨£¬ÃuÃuµÄÓeÄaÃuµÄ£¨²»¿ÉÒÔͬʱ³oÏÖÔÚÒ»¸oÓi¾aÖУ(C)¡£È»ºoÄa¿ÉÒÔʹÓÃbindValueÀ´ÇýøÄaµÄÊaÈe£º
$statement->bindValue(1, 'Cake');
$statement->bindValue(2, true);
$statement2->bindValue(':name', 'Pie');
$statement2->bindValue(':healthy', false);
×¢ÒaʹÓÃÃuÃu²ÎÊýµÄʱºoÄaÒª°uº¬½øðºÅ(:)¡£PDO»¹ÓÐÒ»¸obindParam½¨£¬¿ÉÒÔͨ¹ýÒýÓðo¶¨ÊýÖµ£¬Ò²¾ÍÊÇ˵ËuÖ»ÔÚÓi¾aÖ´ÐеÄʱºo²eÕÒÏaÓ¦ÊýÖµ¡£
ÏÖÔÚʣϵÄΨһҪ×oµÄÊÂÇe£¬¾ÍÊÇÖ´ÐÐÎÒÃǵÄÓi¾a£º
$statement->execute();
$statement2->execute();
// Get our results:
$cake = $statement->Fetch();
$pie = $statement2->Fetch();
ΪÁ˱ÜÃaֻʹÓÃbindValue´øÀ´µÄ´uÂeËeƬ£¬Äa¿ÉÒÔÓÃÊý×e¸øexecute½¨×÷Ϊ²ÎÊý£¬ÏñÕaÑu£º
$statement->execute(array(1 => 'Cake', 2 => true));
$statement2->execute(array(':name' => 'Pie', ':healthy' => false));
ÊÂÎñ
Ç°ÃaeÎÒÃÇÒѾÃeÊo¹ýÁËʲôÊÇÊÂÎñ£º
Ò»¸oÊÂÎñ¾ÍÊÇÖ´ÐÐÒ»×e²eѯ,µ«ÊDz¢²»±£´aeËuÃǵÄÓ°Ïiµ½Êý¾Ý¿aÖС£ÕaÑu×oµÄºÃ´¦ÊÇÈç¹uÄaÖ´ÐÐÁË4ÌoÏa»¥ÒÀÀµµÄ²aÈeÓi¾a,µ±ÓÐÒ»Ìoʧ°Üºo,Äa¿ÉÒԻعoʹµÃÆaËuµÄÊý¾Ý²»Äܹ»²aÈeµ½Êý¾Ý¿aÖÐ,ȱ£Ïa»¥ÒÀÀµµÄ×Ö¶ÎÄܹ»ÕýȵIJaÈe¡£ÄaÐeÒªÈ*±£ÄaʹÓõÄÊý¾Ý¿aÒýÇaeÖ§³ÖÊÂÎñ¡£
¿ªÆoÊÂÎñ
Äa¿ÉÒԺܼoµ¥µÄʹÓÃbeginTransaction()½¨¿ªÆoÒ»¸oÊÂÎñ£º
$db->beginTransaction();
$db->inTransaction(); // true!
È»ºoÄa¿ÉÒÔ¼ÌÐøÖ´ÐÐÄaµÄÊý¾Ý¿a²Ù×÷Ói¾a£¬ÔÚ×iºoÌa½»ÊÂÎñ£º
$db->commit();
»¹ÓÐÀaËÆMySQLiÖеÄrollBack()½¨£¬µ«ÊÇËu²¢²»ÊǻعoËuÓеÄÀaÐÍ£¨ÀýÈçÔÚMySQLÖÐʹÓÃDROP TABLE£(C),Õa¸o½¨²¢²»ÊÇÕaeÕýµÄ¿É¿¿£¬ÎÒ½¨Òe¾¡Á¿±ÜÃaÒÀÀµ´Ë½¨¡£
ÆaËuÓÐÓõÄÑ¡Ïi
Óм¸¸oÑ¡ÏiÄa¿ÉÒÔ¿¼ÂÇÓÃһϡ£ÕaÐ(C)¿ÉÒÔ×÷ΪÄaµÄ¶ÔÏo³oʼ»¯Ê±ºoµÄµÚËĸo²ÎÊýÊaÈe¡£
$options = array($option1 => $value1, $option[..]);
$db = new PDO($dsn, $username, $password, $options);
PDO::ATTR_DEFAULT_FETCH_MODE
Äa¿ÉÒÔÑ¡ÔñPDO½«µ»ØµÄÊÇʲôÀaÐ͵Ľa¹u¼¯£¬ÈçPDO::FETCH_ASSOC£¬»aÔÊÐiÄaʹÓÃ$result['column_name']£¬»oÕßPDO::FETCH_OBJ£¬»aµ»ØÒ»¸oÄaÃu¶ÔÏo£¬ÒÔ±aÄaʹÓÃ$result->column_name
Äa»¹¿ÉÒÔ½«½a¹u*ÅÈeÒ»¸oÌض¨µÄÀa£¨Ä£ÐÍ£(C)£¬¿ÉÒÔͨ¹ý¸øÿһ¸oµ¥¶ÀµÄ²eѯÉeÖÃÒ»¸o¶Áȡģʽ£¬¾ÍÏñÕaÑu£º
$query = $db->query('SELECT * FROM `foods`');
$foods = $query->fetchAll(PDO::FETCH_CLASS, 'Food');
**PDO::ATTR_ERRMODE
**
ÉÏÃaeÎÒÃÇÒѾ½aÊ͹ýÕaÒ»ÌoÁË£¬µ«Ï²»¶TryCatchµÄÈËÐeÒªÓõ½£ºPDO::ERRMODE_EXCEPTION¡£Èç¹u²»ÂÛʲôÔÒoÄaÏeÅ׳oPHP¾¯¸ae£¬¾ÍʹÓÃPDO::ERRMODE_WARNING¡£
PDO::ATTR_TIMEOUT
µ±ÄaΪÔØÈeʱ¼a¶ø׿±Ê±,Äa¿ÉÒÔʹÓôËÊoÐÔÀ´ÎªÄaµÄ²eѯָ¶¨Ò»¸o³¬Ê±Ê±¼a£¬µ¥Î»ÊÇÃe. ×¢Òa£¬Èç¹u³¬¹ýÄaÉeÖõÄʱ¼a£¬È±Ê¡»aÅ׳oE_WARNINGÒi³£, ³ý*Ç PDO::ATTR_ERRMODE ±»¸Ä±a.
¸u¶aÊoÐÔÐÅÏ¢¿ÉÒÔÔÚ PHP¹ÙÍøµÄÊoÐÔÉeÖà Ài²e¿´µ½.
×iºoµÄ˼¿¼
PDOÊÇÒ»¸oÔÚPHPÖÐÃÎÊÄaµÄÊý¾Ý¿aµÄºÜ°oµÄ½Ê½£¬¿ÉÒÔÈÏΪÊÇ×iºÃµÄ½Ê½¡£³ýÇÄa¾Ü¾øʹÓÃÃaeÏo¶ÔÏoµÄ½¨»oÊÇÌ«Ï°¹ß MySQLi µÄ½¨Ãu³Æ£¬*ñÔoûÓÐÀiÓɲ»Ê¹ÓÃPDO¡£
¸uºÃµÄÊÇÍeÈ«Çл»µ½Ö»Ê¹ÓÃÔ¤´¦ÀiÓi¾a£¬Õa×iÖÕ½«Ê¹ÄaµÄÉu»i¸uÇaËÉ£¡