怎樣串連SAE的資料庫
求問..還是上次拆架構那個事,需要串連SAE的資料庫,真心沒自己設過資料庫阿.狂汗.
sae那裡就給了這個sae.class.php類, 該腫麼用?...............定義資料顯示是要另立一個資料COM檔案呢?還是就寫在這個類裡?
拆架構真心太悲劇樂, MVC結構,入口檔案->資料com層->mod層->pageletes層->表現層->modul層,一層套一層, 我全部給include到入口檔案裡了,這樣寫對不對?感覺很絕望. 不知道該進繼續拆好,還是退一步重新使用架構好.
Sae Mysql Class
$mysql = new SaeMysql();
$sql = "SELECT * FROM `user` LIMIT 10";
$data = $mysql->getData( $sql );
$name = strip_tags( $_REQUEST['name'] );
$age = intval( $_REQUEST['age'] );
$sql = "INSERT INTO `user` ( `name` , `age` , `regtime` ) VALUES ( '" . $mysql->escape( $name ) . "' , '" . intval( $age ) . "' , NOW() ) ";
$mysql->runSql( $sql );
if( $mysql->errno() != 0 )
{
die( "Error:" . $mysql->errmsg() );
}
$mysql->closeDb();
?>
------解決方案--------------------
再來一個...嘿嘿
PHP code
arrSql[] = $sql; $result = $this->conn->getData($sql); if( $this->conn->errno() )spError("{$sql}
執行錯誤: " . $this->conn->error()); return $result; } /** * 返回當前插入記錄的主鍵ID */ public function newinsertid() { return $this->conn->lastId(); } /** * 格式化帶limit的SQL語句 */ public function setlimit($sql, $limit) { return $sql. " LIMIT {$limit}"; } /** * 執行一個SQL語句 * * @param sql 需要執行的SQL語句 */ public function exec($sql) { $this->arrSql[] = $sql; $result = $this->conn->runSql($sql); if( $this->conn->errno() )spError("{$sql}
執行錯誤: " . $this->conn->error()); return $result; } /** * 返回影響行數 */ public function affected_rows() { return FALSE; // SAE環境暫時無法擷取影響行數 } /** * 擷取資料表結構 * * @param tbl_name 表名稱 */ public function getTable($tbl_name) { return $this->getArray("DESCRIBE {$tbl_name}"); } /** * 建構函式 * * @param dbConfig 資料庫配置 */ public function __construct($dbConfig) { if(TRUE == SP_DEBUG)sae_set_display_errors(TRUE); $this->conn = new SaeMysql(); if( $this->conn->errno() )spError("資料庫連結錯誤 : " . $this->conn->error()); $this->conn->setCharset("UTF8"); } /** * 對特殊字元進行過濾 * * @param value 值 */ public function __val_escape($value, $quotes = FALSE) { if(is_null($value))return 'NULL'; if(is_bool($value))return $value ? 1 : 0; if(is_int($value))return (int)$value; if(is_float($value))return (float)$value; if(@get_magic_quotes_gpc())$value = stripslashes($value); return '\''.$this->conn->escape($value).'\''; } /** * 解構函式 */ public function __destruct() { @$this->conn->closeDb(); } /** * getConn 取得Sae MySQL對象 * 為了更好地使用Sea提供MySQL類,getSeaDB函數將返回Sae MySQL對象供開發人員使用 */ public function getConn() { return $this->conn; }}