にwpdbクラスを使用して外部データベースへのアクセス。は、私は外部のデータベースに頼っており、この目的のためにwpdbクラスを使用しているのWordPressでカスタムページテンプレートを持っているワードプレス
これは私のコードです:これはそれを行うための正しい方法であれば
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title('<h1 class="entry-title">', '</h1>'); ?>
</header><!-- .entry-header -->
<?php
class StudentsDatabase
{
private $db;
public function __construct() {
try {
$this->db = new wpdb(DB_USER, DB_PASSWORD, 'students_db', DB_HOST);
$this->db->show_errors();
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function getStudentById($student_id)
{
return $this->db->get_results("SELECT * FROM `students` WHERE id=$student_id");
}
public function getSchoolByAreaCode($area_code)
{
return $this->db->get_results("SELECT * FROM `schools` WHERE area_code=$area_code;--");
}
}
$Students_DB = new StudentsDatabase();
$student_one = $Students_DB->getStudentById(1);
$school_one = $Students_DB->getSchoolByAreaCode(1);
?>
<div class="entry-content">
<?php
//do something with $student_one and $school_one ...
the_content();
?>
</div><!-- .entry-content -->
まあ、私は思っていました。セキュリティ上、または「その他」は実際には賢明です。
これは、ページのテンプレート自体の内部から外部DBの呼び出しを行うためにちょっと大雑把な感じ。これらの関数をいくつかの外部ファイルに登録し、それをテンプレートの中で使うべきですか?
私はこれをarea_codeまたはstudent_idの値として入力できますか? "ヌルか1 = 1;テーブルのユーザを落とす - ' "あなたのページに[bobby tables](https://xkcd.com/327/)が手を出す前に、入力が 'サニタイズされている'ことを確認してください。 – coderatchet
@thenaglecodeそれらはすべてうまく消毒されています。私はちょうどコードの一部を投稿しました(しかし、懸念してくれてありがとうございます!) –