求问..还是上次拆框架那个事,需要连接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;
}
}