을 다음과 같이
코드입니다. 당신이 그것을 모르는 경우에, 그것을 배우십시오. 아마 이것은 당신을 시작할 것입니다 :
/*
This the actual SQL query the "?" will be replaced with the values, and escaped accordingly
- ie. you dont need to use the equiv of mysql_real_escape_string - its going to do it
autmatically
*/
$sql = "insert into message2 (mid, seq, created_on_ip, created_by, body) values (?, ?, ?, ?, ?)";
// these are the values that will replace the ?
$args = array($mid, $seq, '1.2.2.1', $currentUser, $body);
// create a prepared statement object
$stmt = $PDO->prepare($sql);
// execute the statement with $args passed in to be used in place of the ?
// so the final query looks something like:
// insert into message2 (mid, seq, created_on_ip, created_by, body) values ($mid, $seq, 1.2.2.1, $currentUser, $body)
$stmt->execute($args);
if (empty($mid)) {
// $mid id is the value of the primary key for the last insert
$mid = $PDO->lastInsertId();
}
// create the first part of another query
$insertSql = "insert into message2_recips values ";
// an array for placeholders - ie. ? in the unprepared sql string
$holders = array();
// array for the params we will pass in as values to be substituted for the ?
$params = array();
// im not sure what the $rows are, but it looks like what we will do is loop
// over a recordset of related rows and do additional inserts based upon them
foreach ($rows as $row) {
// add a place holder string for this row
$holders[] = "(?, ?, ?, ?)";
// assign params
$params[] = $mid;
$params[] = $seq;
$params[] = $row['uid'];
$params[] = $row['uid'] == $currentUser ? 'A' : 'N';
}
// modify the query string to have additional place holders
// so if we have 3 rows the query will look like this:
// insert into message2_recips values (?, ?, ?, ?),(?, ?, ?, ?),(?, ?, ?, ?)
$insertSql .= implode(',', $holders);
// create a prepared statment
$stmt = $PDO->prepare($insertSql);
// execute the statement with the params
$stmt->execute($params);
PDO 정말 좋습니다. 그것은 MySQLi와 동일한 기능을하지만 DB 드라이버 (즉, SQL이 다른 데이터베이스와 호환되는 한 이론적으로 mysql, sqlite, postresql 등과 동일한 PHP 코드를 사용할 수 있음)와 일관된 인터페이스를 제공합니다. prepared statement에 대한 더 나은 매개 변수 바인딩. 이후로 당신은 어떤 식 으로든 mysql
확장을 사용하지 않아야하고, MySQLi는 PHP의 이전 버전을 특별히 지원하지 않는 한 PDO보다 다루기가 더 어렵습니다.
PDO 모듈은 "일반 코드"가 아닙니다. 정확히 무엇으로 변환 하시겠습니까? – Carpetsmoker
아마도 PDO에서 MySQL/MySQLi로 – Joe
아, 죄송합니다 ... 저는 mysql 문에 익숙합니다. – BigJobbies