2022-11-15 09:51来源:m.sf1369.com作者:宇宇
软件安装完后,安装目录下有个WWW文件夹,把项目及你的框架文件放到这个目录下就行。TP框架的引入文件要改好。
当采用原生态的sql语句进行写入操作的时候,要用execute,读操作要用query。
MySQL数据主从同步还是要靠MySQL的机制来实现,所以这个时候MySQL主从同步的延迟问题是需要优化,延迟时间太长不仅影响业务,还影响用户体验。
thinkphp核心类Thinkphp/library/Model.class.php 中,query 方法
调用Thinkphp/library/Think/Db/Driver/Mysql.class.php
/**
* SQL查询
* @access public
* @param string $sql SQL
* @param mixed $parse 是否需要解析SQL
* @return mixed
*/
public function query($sql,$parse=false) {
if(!is_bool($parse) && !is_array($parse)) {
$parse = func_get_args();
array_shift($parse);
}
$sql = $this->parseSql($sql,$parse);
return $this->db->query($sql);
}
调用Thinkphp/library/Think/Db/Driver/Mysql.class.php
/**
* 执行查询 返回数据集
* @access public
* @param string $str sql指令
* @return mixed
*/
public function query($str) {
if(0===stripos($str, 'call')){ // 存储过程查询支持
$this->close();
$this->connected = false;
}
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//释放前次的查询结果
if ( $this->queryID ) { $this->free(); }
N('db_query',1);
// 记录开始执行时间
G('queryStartTime');
$this->queryID = mysql_query($str, $this->_linkID);
$this->debug();
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = mysql_num_rows($this->queryID);
return $this->getAll();
}
}
上面初始化数据库链接时,initConnect(false),调用Thinkphp/library/Think/Db/Db.class.php,注意false、true代码实现。true表示直接调用主库,false表示调用读写分离的读库。
/**
* 初始化数据库连接
* @access protected
* @param boolean $master 主服务器
* @return void
*/
protected function initConnect($master=true) {
if(1 == C('DB_DEPLOY_TYPE'))
// 采用分布式数据库
$this->_linkID = $this->multiConnect($master);
else
// 默认单数据库
if ( !$this->connected ) $this->_linkID = $this->connect();
}
/**
* 连接分布式服务器
* @access protected
* @param boolean $master 主服务器
* @return void
*/
protected function multiConnect($master=false) {
foreach ($this->config as $key=>$val){
$_config[$key] = explode(',',$val);
}
// 数据库读写是否分离
if(C('DB_RW_SEPARATE')){
// 主从式采用读写分离
if($master)
// 主服务器写入
$r = floor(mt_rand(0,C('DB_MASTER_NUM')-1));
else{
if(is_numeric(C('DB_SLAVE_NO'))) {// 指定服务器读
$r = C('DB_SLAVE_NO');
}else{
// 读操作连接从服务器
Thinkphp只是框架,后台要自己实现。
如果你想直接用开源系统的话,可以考虑onethink 它用的也是thinkphp框架。
您好,一般程序商发布的新版本都会兼容支持PHP7,ThinkPHP5~6新版本应该仅支持PHP7.x系。
你是说做个简单的项目吗?
首先你得安装配置好服务器。现在网上有很多集成包,比如wampserver。安装完后里面有很多服务器,一般用Apache。
现在打开phpstorm,如果你会thinkPHP之类的框架,将他的模板复制到你的打开目录下
像我的打开入径是
之后在里面写项目就行了
比较会起问题的是Apache出错,一般phpstorm没有配置的话右下角会提示你配置,你直接配置就可以在右上角点击打开,如果没有配置的话直接在网页上走localhost/项目名 就可以了