//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
public function get_wisher_id_by_name ($name) {
$name = $this->real_escape_string($name);
$wisher = $this->query("SELECT id FROM wishers WHERE name = '"
. $name . "'");
if ($wisher->num_rows > 0){ $row = $wisher->fetch_row(); return $row[0]; } else return null;
}
Oracle データベースの場合:
public function get_wisher_id_by_name($name) {
$query = "SELECT id FROM wishers WHERE name = :user_bv";
$stid = oci_parse($this->con, $query);
oci_bind_by_name($stid, ':user_bv', $name);
oci_execute($stid);
//Because user is a unique value I only expect one row
$row = oci_fetch_array($stid, OCI_ASSOC); if ($row) return $row["ID"]; else return null;
}
コードブロックは、クエリー SELECT ID FROM wishers WHERE name = [ウィッシャーの名前を表す変数] を実行します。クエリーの結果は、クエリーに一致するレコードの ID の配列です。配列が空でない場合は、自動的に要素を 1 つ含むことを意味します。これは、表の作成時にフィールド名が UNIQUE として指定されたためです。この場合、関数は $result 配列の最初の要素を返します (番号が 0 の要素)。配列が空の場合、null を返します。
セキュリティー上の注意: MySQL データベースの場合、$name 文字列は、SQL インジェクション攻撃を避けるためにエスケープされます。SQL インジェクションに関する Wikipedia および mysql_real_escape_string のドキュメントを参照してください。このチュートリアルのコンテキストでは、有害な SQL インジェクションのリスクはありませんが、そのような攻撃のリスクになるような MySQL クエリーの文字列はエスケープするのがベストプラクティスです。Oracle データベースでは、バインド変数を使用してこの問題を回避しています。
関数 get_wishes_by_wisher_id
この関数は、入力パラメータとしてウィッシャーの ID を必要とし、そのウィッシャーに対して登録されているウィッシュを返します。
次のコードブロックを入力します。
MySQL データベースの場合:
public function get_wishes_by_wisher_id($wisherID) { return $this->query("SELECT id, description, due_date FROM wishes WHERE wisher_id=" . $wisherID); }
Oracle データベースの場合:
public function get_wishes_by_wisher_id($wisherID) {
$query = "SELECT id, description, due_date FROM wishes WHERE wisher_id = :id_bv";
$stid = oci_parse($this->con, $query);
oci_bind_by_name($stid, ":id_bv", $wisherID);
oci_execute($stid);
return $stid;
}
コードブロックは、クエリー "SELECT id, description, due_date FROM wishes WHERE wisherID=" . $wisherID を実行し、クエリーに一致するレコードの配列である結果セットを返します。(Oracle データベースは、データベースのパフォーマンスとセキュリティーの理由から、バインド変数を使用します。)この選択は、wishes 表の外部キーである wisherID によって実行されます。
データベースに接続してウィッシャーの ID を取得するコードを、get_wisher_id_by_name 関数の呼び出しで置き換えます。
MySQL データベースの場合は、次のようにコードを置き換えます。
$con = mysqli_connect("localhost", "phpuser", "phpuserpw");
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
//set the default client character set
mysqli_set_charset($con, 'utf-8');
mysqli_select_db($con, "wishlist");
$user = mysqli_real_escape_string($con, $_GET['user']);
$wisher = mysqli_query($con, "SELECT id FROM wishers WHERE name='" . $user . "'");
if (mysqli_num_rows($wisher) < 1) {
exit("The person " . $_GET['user'] . " is not found. Please check the spelling and try again");
}
$row = mysqli_fetch_row($wisher); $wisherID = $row[0];
mysqli_free_result($wisher);
$wisherID = WishDB::getInstance()->get_wisher_id_by_name($_GET["user"]);
if (!$wisherID) {
exit("The person " .$_GET["user"]. " is not found. Please check the spelling and try again" );
}
Oracle データベースの場合は、次のようにコードを置き換えます。
$con = oci_connect("phpuser", "phpuserpw", "localhost/XE", "AL32UTF8");
if (!$con) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
$query = "SELECT id FROM wishers WHERE name = :user_bv";
$stid = oci_parse($con, $query);
$user = $_GET["user"];
oci_bind_by_name($stid, ':user_bv', $user);
oci_execute($stid);
//Because user is a unique value I only expect one row
$row = oci_fetch_array($stid, OCI_ASSOC);
if (!$row) {
echo("The person " . $user . " is not found. Please check the spelling and try again" ); exit;
}
$wisherID = $row["ID"]; $wisherID = WishDB::getInstance()->get_wisher_id_by_name($_GET["user"]);
if (!$wisherID) {
exit("The person " .$_GET["user"]. " is not found. Please check the spelling and try again" );
}