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
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
include_once "/inc/config.php";
$mysqli = get_mysqli_connection();
include_once dirname(__FILE__) . "/inc/getBasic.inc.php";
include_once dirname(__FILE__) . "/inc/getData.inc.php";
include_once dirname(__FILE__) . "/inc/tradeRec.inc.php";
+2 -1
View File
@@ -1,6 +1,7 @@
<?php
ini_set("display_errors","0");
$mysqli = new mysqli('localhost','myquant','_H(lU1_fF*9baRTp','myquant');
include_once __DIR__."/config.php";
$mysqli = new mysqli($db_config['host'],$db_config['username'],$db_config['password'],$db_config['database']);
//ajax: get stock list for select
if($_REQUEST['t']=='stockList'){
+27
View File
@@ -0,0 +1,27 @@
<?php
function get_db_config() {
return [
'host' => '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;
}
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
require_once "vendor/autoload.php";
include_once __DIR__."/config.php";
use PhpOffice\PhpSpreadsheet\Reader\Csv;
/**
@@ -26,7 +27,7 @@ function readMyExcel($file): array
*/
function dbOp($data,$company)
{
$mysqli = new mysqli('localhost', 'root', 'nancysimon', 'myquant');
$mysqli = get_mysqli_connection();
$maxrow = count($data);
$maxcol = count($data[0]);
$msg = array();
+7 -45
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,7 +13,7 @@ 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');
$mysqli = get_mysqli_connection();
$where = " and trade_date>'".$day_st."'";
if($day_end) {
$day_end=date('Ymd',strtotime($day_end));
@@ -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
+5 -5
View File
@@ -1,5 +1,5 @@
<?php
include_once __DIR__."/config.php";
/*
上证指数:000001.SH
深证成指:399001.SZ
@@ -7,7 +7,7 @@
创业板指:399006.SZ
*/
function getIndexData($ts_code,$day_st,$day_end){
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
#$ts_code = '000001.SH';
#$day_st = '20000101';
@@ -43,7 +43,7 @@ return array('tDate'=>$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';
+2 -2
View File
@@ -1,7 +1,7 @@
<?php
include_once __DIR__."/config.php";
function getEstateData($city='宁波',$fDate,$toDate){
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
$sql=<<<EOF
SELECT city, listdate,sum(nums) as num FROM `estate_listing`
where listdate>='$fDate' and listdate<='$toDate' and city='$city' group by listdate
+2 -1
View File
@@ -1,8 +1,9 @@
<?php
ini_set("display_errors","1");
include_once "/inc/config.php";
//ini_set('error_reporting', 'E_ALL'); //wrong way to open error_reporting
//ini_set("error_reporting","32759"); //Right way to set error_reporting
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
include_once __DIR__ . "/inc/excelOperate.inc.php";
include_once __DIR__ . "/inc/tradeRec.inc.php";
-1
View File
@@ -3,7 +3,6 @@ include_once "inc/getBasic.inc.php";
include_once "inc/getData.inc.php";
include_once "inc/postJson.inc.php";
include_once "inc/getEstate.inc.php";
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2023-06-20';
$_REQUEST['t_end']=$_REQUEST['t_end']?$_REQUEST['t_end']:date('Y-m-d');
+2 -1
View File
@@ -1,10 +1,11 @@
<?php
ini_set("display_errors","0");
include_once "/inc/config.php";
include_once dirname(__FILE__) . "/inc/getBasic.inc.php";
include_once dirname(__FILE__) . "/inc/tradeRec.inc.php";
include_once __DIR__ . "/inc/postJson.inc.php";
//include_once dirname(__FILE__) . "/class/basic_info.class.php";
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
$mysqli = get_mysqli_connection();
$_REQUEST['t_vendor']=($_REQUEST['t_vendor'])?$_REQUEST['t_vendor']:'方正证券';
if($_REQUEST['ts_code']) {
$_REQUEST['t_start'] = $_REQUEST['t_start'] ? $_REQUEST['t_start'] : '2020-01-01';
+1
View File
@@ -1,4 +1,5 @@
<?php
ini_set("display_errors","0");
include_once "inc/getBasic.inc.php";
$_REQUEST['ts_code']=$_REQUEST['ts_code']?$_REQUEST['ts_code']:'000001';
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2015-01-01';