Pear DB 更新

投稿者:

<?php
//===== パラメータ読み込み(以降CFG_*は当ファイルにより設定された定数)
require_once (‘include/param.inc.php’);
require_once (‘include/pear/DB.php’); // ★★★
//===== DB接続
$db = DB::connect (“mysql://”.CFG_MYSQL_USERNAME.”:”.CFG_MYSQL_PASSWORD.”@”.CFG_MYSQL_HOST.”/CFG_MYSQL_DBNAME”); // ★★★
if (DB::isError( $db )) {
die($db->getMessage());
} // ★★★
//===== 文字化け対策
$sql = “SET NAMES utf8”;
$result = $db->query($sql); // ★★★
//===== インサート用サンプルデータ
$tmani_time = “時間”;
$tmani_from = “フロム”;
$tmani_subject = “文字化け直ってるといいね”;
$tmani_body = “お元気ですか?
how r u?”;
//===== クエリー文の設定
$stt=$db->prepare(“INSERT INTO “.CFG_MYSQL_TABPREFIX.”posts (
post_mail_from,
post_mail_subject,
post_mail_body)
VALUES (?,?,?)”); // ★★★
//===== クエリー文の実行
$res = $db->execute($stt,array($tmani_from,$tmani_subject,$tmani_body)); // ★★★
if( $db->isError($return) ){
$db->rollback();
die($res->getMessage());
}
       $db->commit();
//===== 結果表示
$post_id = mysql_insert_id();
$next_id = $db->nextid($res) // ★★★
if($res) {
echo “成功! \n res: “.$res.” \n post-id: “.$post_id.” \n next-id: “.$next_id; // ★★★
} else {
echo “失敗↓ \n res: “.$res.” \n post-id: “.$post_id.” \n next-id: “.$next_id; // ★★★
}
//===== DBクローズ
$db->disconnect(); // ★★★
?>
こんな風に
プリコンパイル済みSQLの場合は prepare, excute で行う
そうでなければqueryでOK

Thank you for reading this post, don't forget to subscribe!