feat: Refactor database connection handling to use centralized configuration

This commit is contained in:
杨水淼
2025-11-12 16:00:32 +08:00
parent 8303ae39f1
commit 4c348e379d
11 changed files with 86 additions and 92 deletions
+41 -79
View File
@@ -1,5 +1,7 @@
<?php
include_once __DIR__."/config.php";
include_once __DIR__."/functions.inc.php";
/**
* 获取PE_TTM,PB,PS等历史数据
* @param $ts_code ts code
@@ -11,42 +13,42 @@ include_once __DIR__."/functions.inc.php";
function getBasicData($ts_code,$day_st,$day_end,$item){
$day_st=date('Ymd',strtotime($day_st));
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$where = " and trade_date>'".$day_st."'";
if($day_end) {
$day_end=date('Ymd',strtotime($day_end));
$where .= " and trade_date <'".$day_end."'";
}
$sql= <<<EOF
select DATE_FORMAT(trade_date,'%Y-%m-%d') as trade_date, $item as pe_ttm from stock_his_basic_pro where ts_code = '$ts_code' $where order by trade_date asc
EOF;
#echo $sql;
$result = $mysqli->query($sql);
$tDate=$idx=$data=$data_clean=array(); # $data_clean is an array without null
if($result && $result->num_rows>0) {
#$data = $result->fetch_all();
while($row=$result->fetch_assoc()){
$trade_date=$row['trade_date'];
#$trade_date=substr($row['trade_date'],0,4).'-'.substr($row['trade_date'],4,2).'-'.substr($row['trade_date'],6,2);
#$trade_date1=strtotime($row['trade_date']);
array_push($tDate,$trade_date);
array_push($idx,$row['pe_ttm']);
$data_tmp = array("value"=>array($trade_date,$row['pe_ttm']));
array_push($data,$data_tmp);
if($data_tmp['value'][1]>0) array_push($data_clean,$data_tmp['value'][1]);
$mysqli = get_mysqli_connection();
$where = " and trade_date>'".$day_st."'";
if($day_end) {
$day_end=date('Ymd',strtotime($day_end));
$where .= " and trade_date <'".$day_end."'";
}
}
$data_last= round($data_clean[count($data_clean)-1],2);
$data_max= round(max($data_clean),2);
$data_min= round(min($data_clean),2);
if(count($data_clean)>0) $data_avg= round(array_sum($data_clean)/count($data_clean),2);
else $data_avg=null;
#Free result and close connection.
$result->free();
$mysqli->close();
return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data,'data_max'=>$data_max,'data_min'=>$data_min,'data_avg'=>$data_avg,'data_last'=>$data_last);
$sql= <<<EOF
select DATE_FORMAT(trade_date,'%Y-%m-%d') as trade_date, $item as pe_ttm from stock_his_basic_pro where ts_code = '$ts_code' $where order by trade_date asc
EOF;
#echo $sql;
$result = $mysqli->query($sql);
$tDate=$idx=$data=$data_clean=array(); # $data_clean is an array without null
if($result && $result->num_rows>0) {
#$data = $result->fetch_all();
while($row=$result->fetch_assoc()){
$trade_date=$row['trade_date'];
#$trade_date=substr($row['trade_date'],0,4).'-'.substr($row['trade_date'],4,2).'-'.substr($row['trade_date'],6,2);
#$trade_date1=strtotime($row['trade_date']);
array_push($tDate,$trade_date);
array_push($idx,$row['pe_ttm']);
$data_tmp = array("value"=>array($trade_date,$row['pe_ttm']));
array_push($data,$data_tmp);
if($data_tmp['value'][1]>0) array_push($data_clean,$data_tmp['value'][1]);
}
}
$data_last= round($data_clean[count($data_clean)-1],2);
$data_max= round(max($data_clean),2);
$data_min= round(min($data_clean),2);
if(count($data_clean)>0) $data_avg= round(array_sum($data_clean)/count($data_clean),2);
else $data_avg=null;
#Free result and close connection.
$result->free();
$mysqli->close();
return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data,'data_max'=>$data_max,'data_min'=>$data_min,'data_avg'=>$data_avg,'data_last'=>$data_last);
}
@@ -72,7 +74,7 @@ function ts_code_conv($ts_code){
}
function getStockHist($ts_code,$day_st,$day_end){
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
$where = " and trade_date>='".$day_st."'";
if($day_end) $where .= " and trade_date <='".$day_end."'";
$sql= <<<EOF
@@ -100,7 +102,7 @@ return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data);
}
function tscodeToName($ts_code){
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
$sql= "select name from stock_all_pro where ts_code='{$ts_code}'";
$result = $mysqli->query($sql);
@@ -111,7 +113,7 @@ function tscodeToName($ts_code){
}
function getBasicExtData($ts_code,$item,$day_st,$day_end){
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
$where = " and trade_date>'".$day_st."' ";
if($item=='total_mv_all' or $item=='circ_mv_all') {
$where .= "and vol/10000/10000 > 1";
@@ -150,46 +152,6 @@ $mysqli->close();
if(count($idx)>0) $data_avg= round(array_sum($idx)/count($idx),2);
return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data,'data_max'=>$data_max,'data_min'=>$data_min,'data_avg'=>$data_avg,'data_last'=>$data_last);
}
/*
f2: date
f9: share num
f10: share money
f11: share ratio 流动比例
f12: share ratio 总股本比例
*/
/*
function temporary disabled.
TODO: delete if useless confirm.
function getStockIhHist($ts_code,$day_st,$day_end,$item){
$ts_code = ts_code_conv($ts_code);
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$where = " and f2>'".$day_st."'";
if($day_end) $where .= " and f2<'".$day_end."'"; // F2: date
$sql = <<<EOF
select f2, sum(f9) f9,round(sum(f10),2) sumf10,round(sum(f11),2) sumf11 from ih_by_ts_code
where f0 = '$ts_code' $where group by f2 order by f2 asc
EOF;
//EOF必须在行首,且后面不能有空格
$result = $mysqli->query($sql);
$tDate=$idx=$data=array();
if($result && $result->num_rows>0) {
#$data = $result->fetch_all();
while($row=$result->fetch_assoc()){
array_push($tDate,$row['f2']);
array_push($idx,$row['vol']); //$item can choose f9,f10,f11,f12
array_push($data,array("value"=>array($trade_date,$row['vol'])));
}
}
#Free result and close connection.
$result->free();
$mysqli->close();
return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data);
}
*/
/**
* @param $date : given a date (yyyy-mm-dd)
@@ -207,7 +169,7 @@ function dateGap($date,$gap){
* @return bool true
*/
function tradeStocksList($email=null){
global $mysqli;
$mysqli = get_mysqli_connection();
if($_REQUEST['t_vendor']=='方正证券'){
$sql= <<< EOF
select distinct(ts_code) ts_code,ts_name from trade_record