feat: Refactor database connection handling to use centralized configuration
This commit is contained in:
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?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/getBasic.inc.php";
|
||||||
include_once dirname(__FILE__) . "/inc/getData.inc.php";
|
include_once dirname(__FILE__) . "/inc/getData.inc.php";
|
||||||
include_once dirname(__FILE__) . "/inc/tradeRec.inc.php";
|
include_once dirname(__FILE__) . "/inc/tradeRec.inc.php";
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set("display_errors","0");
|
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
|
//ajax: get stock list for select
|
||||||
if($_REQUEST['t']=='stockList'){
|
if($_REQUEST['t']=='stockList'){
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once "vendor/autoload.php";
|
require_once "vendor/autoload.php";
|
||||||
|
include_once __DIR__."/config.php";
|
||||||
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,7 +27,7 @@ function readMyExcel($file): array
|
|||||||
*/
|
*/
|
||||||
function dbOp($data,$company)
|
function dbOp($data,$company)
|
||||||
{
|
{
|
||||||
$mysqli = new mysqli('localhost', 'root', 'nancysimon', 'myquant');
|
$mysqli = get_mysqli_connection();
|
||||||
$maxrow = count($data);
|
$maxrow = count($data);
|
||||||
$maxcol = count($data[0]);
|
$maxcol = count($data[0]);
|
||||||
$msg = array();
|
$msg = array();
|
||||||
|
|||||||
+27
-65
@@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
include_once __DIR__."/config.php";
|
||||||
include_once __DIR__."/functions.inc.php";
|
include_once __DIR__."/functions.inc.php";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取PE_TTM,PB,PS等历史数据
|
* 获取PE_TTM,PB,PS等历史数据
|
||||||
* @param $ts_code ts code
|
* @param $ts_code ts code
|
||||||
@@ -11,21 +13,21 @@ include_once __DIR__."/functions.inc.php";
|
|||||||
function getBasicData($ts_code,$day_st,$day_end,$item){
|
function getBasicData($ts_code,$day_st,$day_end,$item){
|
||||||
$day_st=date('Ymd',strtotime($day_st));
|
$day_st=date('Ymd',strtotime($day_st));
|
||||||
|
|
||||||
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
|
$mysqli = get_mysqli_connection();
|
||||||
$where = " and trade_date>'".$day_st."'";
|
$where = " and trade_date>'".$day_st."'";
|
||||||
if($day_end) {
|
if($day_end) {
|
||||||
$day_end=date('Ymd',strtotime($day_end));
|
$day_end=date('Ymd',strtotime($day_end));
|
||||||
$where .= " and trade_date <'".$day_end."'";
|
$where .= " and trade_date <'".$day_end."'";
|
||||||
}
|
}
|
||||||
$sql= <<<EOF
|
$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
|
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;
|
EOF;
|
||||||
#echo $sql;
|
#echo $sql;
|
||||||
$result = $mysqli->query($sql);
|
$result = $mysqli->query($sql);
|
||||||
|
|
||||||
$tDate=$idx=$data=$data_clean=array(); # $data_clean is an array without null
|
$tDate=$idx=$data=$data_clean=array(); # $data_clean is an array without null
|
||||||
|
|
||||||
if($result && $result->num_rows>0) {
|
if($result && $result->num_rows>0) {
|
||||||
#$data = $result->fetch_all();
|
#$data = $result->fetch_all();
|
||||||
while($row=$result->fetch_assoc()){
|
while($row=$result->fetch_assoc()){
|
||||||
$trade_date=$row['trade_date'];
|
$trade_date=$row['trade_date'];
|
||||||
@@ -37,16 +39,16 @@ if($result && $result->num_rows>0) {
|
|||||||
array_push($data,$data_tmp);
|
array_push($data,$data_tmp);
|
||||||
if($data_tmp['value'][1]>0) array_push($data_clean,$data_tmp['value'][1]);
|
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_last= round($data_clean[count($data_clean)-1],2);
|
||||||
$data_max= round(max($data_clean),2);
|
$data_max= round(max($data_clean),2);
|
||||||
$data_min= round(min($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);
|
if(count($data_clean)>0) $data_avg= round(array_sum($data_clean)/count($data_clean),2);
|
||||||
else $data_avg=null;
|
else $data_avg=null;
|
||||||
#Free result and close connection.
|
#Free result and close connection.
|
||||||
$result->free();
|
$result->free();
|
||||||
$mysqli->close();
|
$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);
|
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){
|
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."'";
|
$where = " and trade_date>='".$day_st."'";
|
||||||
if($day_end) $where .= " and trade_date <='".$day_end."'";
|
if($day_end) $where .= " and trade_date <='".$day_end."'";
|
||||||
$sql= <<<EOF
|
$sql= <<<EOF
|
||||||
@@ -100,7 +102,7 @@ return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data);
|
|||||||
}
|
}
|
||||||
|
|
||||||
function tscodeToName($ts_code){
|
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}'";
|
$sql= "select name from stock_all_pro where ts_code='{$ts_code}'";
|
||||||
$result = $mysqli->query($sql);
|
$result = $mysqli->query($sql);
|
||||||
@@ -111,7 +113,7 @@ function tscodeToName($ts_code){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getBasicExtData($ts_code,$item,$day_st,$day_end){
|
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."' ";
|
$where = " and trade_date>'".$day_st."' ";
|
||||||
if($item=='total_mv_all' or $item=='circ_mv_all') {
|
if($item=='total_mv_all' or $item=='circ_mv_all') {
|
||||||
$where .= "and vol/10000/10000 > 1";
|
$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);
|
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);
|
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)
|
* @param $date : given a date (yyyy-mm-dd)
|
||||||
@@ -207,7 +169,7 @@ function dateGap($date,$gap){
|
|||||||
* @return bool true
|
* @return bool true
|
||||||
*/
|
*/
|
||||||
function tradeStocksList($email=null){
|
function tradeStocksList($email=null){
|
||||||
global $mysqli;
|
$mysqli = get_mysqli_connection();
|
||||||
if($_REQUEST['t_vendor']=='方正证券'){
|
if($_REQUEST['t_vendor']=='方正证券'){
|
||||||
$sql= <<< EOF
|
$sql= <<< EOF
|
||||||
select distinct(ts_code) ts_code,ts_name from trade_record
|
select distinct(ts_code) ts_code,ts_name from trade_record
|
||||||
|
|||||||
+5
-5
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
include_once __DIR__."/config.php";
|
||||||
/*
|
/*
|
||||||
上证指数:000001.SH
|
上证指数:000001.SH
|
||||||
深证成指:399001.SZ
|
深证成指:399001.SZ
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
创业板指:399006.SZ
|
创业板指:399006.SZ
|
||||||
*/
|
*/
|
||||||
function getIndexData($ts_code,$day_st,$day_end){
|
function getIndexData($ts_code,$day_st,$day_end){
|
||||||
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
|
$mysqli = get_mysqli_connection();
|
||||||
|
|
||||||
#$ts_code = '000001.SH';
|
#$ts_code = '000001.SH';
|
||||||
#$day_st = '20000101';
|
#$day_st = '20000101';
|
||||||
@@ -43,7 +43,7 @@ return array('tDate'=>$tDate,'idx'=>$idx,'data'=>$data);
|
|||||||
#share: vposition 市值 f10, sahreHDnum 股数 f9
|
#share: vposition 市值 f10, sahreHDnum 股数 f9
|
||||||
*/
|
*/
|
||||||
function getIhData($lx,$share,$day_st,$day_end){
|
function getIhData($lx,$share,$day_st,$day_end){
|
||||||
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
|
$mysqli = get_mysqli_connection();
|
||||||
/*
|
/*
|
||||||
$where = " and rdate>='".$day_st."'";
|
$where = " and rdate>='".$day_st."'";
|
||||||
if($day_end) $where .= " and rdate<='".$day_end."'";
|
if($day_end) $where .= " and rdate<='".$day_end."'";
|
||||||
@@ -176,7 +176,7 @@ function hkholdConv($code){
|
|||||||
#LX: tp字段: 序号 1-6,分别代表基金,QFII, 社保,券商,保险,信托
|
#LX: tp字段: 序号 1-6,分别代表基金,QFII, 社保,券商,保险,信托
|
||||||
*/
|
*/
|
||||||
function getIhDataByStock($ts_code,$item,$lx,$day_st,$day_end){
|
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."'";
|
$where = " and ih_date>'".$day_st."'";
|
||||||
if($day_end) $where .= " and ih_date<'".$day_end."'";
|
if($day_end) $where .= " and ih_date<'".$day_end."'";
|
||||||
if($lx) $where .= " and tp='".$lx."'";
|
if($lx) $where .= " and tp='".$lx."'";
|
||||||
@@ -219,7 +219,7 @@ EOF;
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getMoneyFlowData($itm, $day_st,$day_end, $stacked){
|
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';
|
if($stacked==1) $tb = 'moneyflow_hsgt_pro_ext';
|
||||||
else $tb = 'moneyflow_hsgt_pro';
|
else $tb = 'moneyflow_hsgt_pro';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
include_once __DIR__."/config.php";
|
||||||
function getEstateData($city='宁波',$fDate,$toDate){
|
function getEstateData($city='宁波',$fDate,$toDate){
|
||||||
$mysqli = new mysqli('localhost','root','nancysimon','myquant');
|
$mysqli = get_mysqli_connection();
|
||||||
$sql=<<<EOF
|
$sql=<<<EOF
|
||||||
SELECT city, listdate,sum(nums) as num FROM `estate_listing`
|
SELECT city, listdate,sum(nums) as num FROM `estate_listing`
|
||||||
where listdate>='$fDate' and listdate<='$toDate' and city='$city' group by listdate
|
where listdate>='$fDate' and listdate<='$toDate' and city='$city' group by listdate
|
||||||
|
|||||||
+2
-1
@@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set("display_errors","1");
|
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', 'E_ALL'); //wrong way to open error_reporting
|
||||||
//ini_set("error_reporting","32759"); //Right way to set 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/excelOperate.inc.php";
|
||||||
include_once __DIR__ . "/inc/tradeRec.inc.php";
|
include_once __DIR__ . "/inc/tradeRec.inc.php";
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ include_once "inc/getBasic.inc.php";
|
|||||||
include_once "inc/getData.inc.php";
|
include_once "inc/getData.inc.php";
|
||||||
include_once "inc/postJson.inc.php";
|
include_once "inc/postJson.inc.php";
|
||||||
include_once "inc/getEstate.inc.php";
|
include_once "inc/getEstate.inc.php";
|
||||||
|
|
||||||
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2023-06-20';
|
$_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');
|
$_REQUEST['t_end']=$_REQUEST['t_end']?$_REQUEST['t_end']:date('Y-m-d');
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
ini_set("display_errors","0");
|
ini_set("display_errors","0");
|
||||||
|
include_once "/inc/config.php";
|
||||||
include_once dirname(__FILE__) . "/inc/getBasic.inc.php";
|
include_once dirname(__FILE__) . "/inc/getBasic.inc.php";
|
||||||
include_once dirname(__FILE__) . "/inc/tradeRec.inc.php";
|
include_once dirname(__FILE__) . "/inc/tradeRec.inc.php";
|
||||||
include_once __DIR__ . "/inc/postJson.inc.php";
|
include_once __DIR__ . "/inc/postJson.inc.php";
|
||||||
//include_once dirname(__FILE__) . "/class/basic_info.class.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']:'方正证券';
|
$_REQUEST['t_vendor']=($_REQUEST['t_vendor'])?$_REQUEST['t_vendor']:'方正证券';
|
||||||
if($_REQUEST['ts_code']) {
|
if($_REQUEST['ts_code']) {
|
||||||
$_REQUEST['t_start'] = $_REQUEST['t_start'] ? $_REQUEST['t_start'] : '2020-01-01';
|
$_REQUEST['t_start'] = $_REQUEST['t_start'] ? $_REQUEST['t_start'] : '2020-01-01';
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
ini_set("display_errors","0");
|
||||||
include_once "inc/getBasic.inc.php";
|
include_once "inc/getBasic.inc.php";
|
||||||
$_REQUEST['ts_code']=$_REQUEST['ts_code']?$_REQUEST['ts_code']:'000001';
|
$_REQUEST['ts_code']=$_REQUEST['ts_code']?$_REQUEST['ts_code']:'000001';
|
||||||
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2015-01-01';
|
$_REQUEST['t_start']=$_REQUEST['t_start']?$_REQUEST['t_start']:'2015-01-01';
|
||||||
|
|||||||
Reference in New Issue
Block a user