From 4c348e379d38984547943b3fb115ed48fbbb152d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B0=B4=E6=B7=BC?= Date: Wed, 12 Nov 2025 16:00:32 +0800 Subject: [PATCH] feat: Refactor database connection handling to use centralized configuration --- chartStDetail.php | 3 +- inc/ajax.inc.php | 3 +- inc/config.php | 27 +++++++++ inc/excelOperate.inc.php | 3 +- inc/getBasic.inc.php | 120 +++++++++++++-------------------------- inc/getData.inc.php | 10 ++-- inc/getEstate.inc.php | 4 +- myexcel.php | 3 +- realestate.php | 1 - stockTradeRecord.php | 3 +- stock_trend.php | 1 + 11 files changed, 86 insertions(+), 92 deletions(-) create mode 100644 inc/config.php diff --git a/chartStDetail.php b/chartStDetail.php index d9ed507..f49a810 100644 --- a/chartStDetail.php +++ b/chartStDetail.php @@ -1,5 +1,6 @@ 'localhost', + 'username' => 'myquant', + 'password' => '_H(lU1_fF*9baRTp', + 'database' => 'myquant', + 'charset' => 'utf8mb4' + ]; +} + +function get_mysqli_connection() { + $db_config = get_db_config(); + $mysqli = new mysqli( + $db_config['host'], + $db_config['username'], + $db_config['password'], + $db_config['database'] + ); + + if ($mysqli->connect_error) { + die("Connection failed: " . $mysqli->connect_error); + } + + $mysqli->set_charset($db_config['charset']); + return $mysqli; +} \ No newline at end of file diff --git a/inc/excelOperate.inc.php b/inc/excelOperate.inc.php index a3e0039..f049b10 100644 --- a/inc/excelOperate.inc.php +++ b/inc/excelOperate.inc.php @@ -1,5 +1,6 @@ '".$day_st."'"; -if($day_end) { - $day_end=date('Ymd',strtotime($day_end)); - $where .= " and trade_date <'".$day_end."'"; -} -$sql= <<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= <<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= <<$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 = <<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 diff --git a/inc/getData.inc.php b/inc/getData.inc.php index f9068d3..b6ac3b0 100644 --- a/inc/getData.inc.php +++ b/inc/getData.inc.php @@ -1,5 +1,5 @@ $tDate,'idx'=>$idx,'data'=>$data); #share: vposition 市值 f10, sahreHDnum 股数 f9 */ function getIhData($lx,$share,$day_st,$day_end){ - $mysqli = new mysqli('localhost','root','nancysimon','myquant'); + $mysqli = get_mysqli_connection(); /* $where = " and rdate>='".$day_st."'"; if($day_end) $where .= " and rdate<='".$day_end."'"; @@ -176,7 +176,7 @@ function hkholdConv($code){ #LX: tp字段: 序号 1-6,分别代表基金,QFII, 社保,券商,保险,信托 */ function getIhDataByStock($ts_code,$item,$lx,$day_st,$day_end){ - $mysqli = new mysqli('localhost','root','nancysimon','myquant'); + $mysqli = get_mysqli_connection(); $where = " and ih_date>'".$day_st."'"; if($day_end) $where .= " and ih_date<'".$day_end."'"; if($lx) $where .= " and tp='".$lx."'"; @@ -219,7 +219,7 @@ EOF; } function getMoneyFlowData($itm, $day_st,$day_end, $stacked){ - $mysqli = new mysqli('localhost','root','nancysimon','myquant'); + $mysqli = get_mysqli_connection(); if($stacked==1) $tb = 'moneyflow_hsgt_pro_ext'; else $tb = 'moneyflow_hsgt_pro'; diff --git a/inc/getEstate.inc.php b/inc/getEstate.inc.php index 7d5b33b..8344e28 100644 --- a/inc/getEstate.inc.php +++ b/inc/getEstate.inc.php @@ -1,7 +1,7 @@ ='$fDate' and listdate<='$toDate' and city='$city' group by listdate diff --git a/myexcel.php b/myexcel.php index 9a9a616..05ffc63 100644 --- a/myexcel.php +++ b/myexcel.php @@ -1,8 +1,9 @@