然後把他物件化實作成功能。
主程式如下:
require("sql_connect.php"); require("class.data_serach.php"); $data_serach = new data_serach("test","sick_member",3); $sql = $data_serach->sql_text();如此就會獲得一個可以用來查詢的sql字串,再看使用者怎麼去用這個字串。
其中資料庫名稱和資料表以及需要多少表單的資料量才能進行搜尋,必須在物件成生時就給
予其值。(因為寫入建構子)
物件檔名:class.data_serach.php
class data_serach{ function __construct($database,$table,$mixtimer){ $this->database = $database; $this->table = $table; $this->mixtimer = $mixtimer; $this->load_field(); $this->post_check(); } function load_field(){ $result = mysql_list_fields($this->database,$this->table); $field_num = mysql_num_fields($result); for ($i = 0;$i <$field_num;$i++){ $fieldarr[] = mysql_field_name($result,$i); } $this->fieldarr = $fieldarr; } function post_check(){ foreach($this->fieldarr as $value){ if (strlen($_POST[$value]) != 0){ $this->where .= "{$value}='{$_POST[$value]}' AND "; $this->timer +=1; } } } function sql_text(){ if (($this->timer >= $this->mixtimer)) return "SELECT * FROM incoming WHERE ".substr($this->where,0,-5); } }