我正在寻找一个
PHP PDO完整的工作示例,具有运行查询和处理错误的最佳做法.这是我到目前为止.
@H_404_14@
Here是PDO的现代起始者指南.它回答了您的一些问题,并解释了很多其他基本的PDO功能.
正在连接.如果您不这样做,连接失败将默认将DB凭据显示给您网站的所有用户.
try { $dbh = new PDO("MysqL:host=localhost;dbname=phor_lang","phor_lang","'9lsnthsn9"); } catch (PDOException $e) { error(false,"PDO ERROR: " . $e->getMessage()); }
用查询
$stmt = $dbh->prepare("INSERT INTO sets"); $stmt->execute() or error(0,"USERS ERROR ".__LINE__." ".print_r($dbh->errorInfo(),true)); $setID = $dbh->lastInsertID(); $stmt->closeCursor(); $stmt = $dbh->prepare("INSERT INTO words (language,name,detail,user,type,set) VALUES (?,?,?)"); $stmt->execute(array($l1l,$l1w,$l1d,$userID,'training',$setID)) or error(0,true)); $stmt->closeCursor();
我昨天刚刚读完,发现它是一个很好的资源!
以下是“错误”下的引用:
try { $conn = new PDO('MysqL:host=localhost;dbname=myDatabase',$username,$password); $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); }