我正在尝试开发一个交易应用程序,我写了这个应该执行买卖订单的PHP
脚本,计划每5分钟运行一次并执行订单表中的所有订单待处理(状态= 1)。
问题是一次只执行一个订单。这是剧本:
public function execute_order(){
$which_sql = "SELECT * FROM orders WHERE status_id = 1 AND price = (SELECT max(price) FROM orders WHERE status_id = 1)";
$which_result = $this->connection ->query($which_sql);
if($which_result->num_rows>0){
while($row=$which_result->fetch_assoc()){
$which=$row["order_type_id"];
$ord_id = $row["ord_id"];
echo "order type id= ".$which."\r\n";
$maxprice = $row["price"];
$volume = $row["vol"];
$ord_id= $row["ord_id"];
$comp = $row["comp"];
$sh_id = $row["sh_id"];
echo "price =". $maxprice."\r\n";
echo "volume= ".$volume."\r\n";
echo "order id = ".$ord_id."\r\n";
echo "comp = ".$comp."\r\n";
echo "sh_id= ".$sh_id."\r\n";
//start of the switch phrase
switch ($which){
//SELL ORDER
case 1:
//the update query
$update_sql = "UPDATE orders SET status_id = 2 WHERE ord_id = $ord_id";
//update execution
if ($this -> connection->query($update_sql) === TRUE){
echo "1. SUCCESSFULL SELL UPDATE \r\n";
}
else{
echo "ERROR SELL UPDATE: " . $this -> connection->error."\r\n";
}
//THE INSERT QUERY
$insert_sql = "INSERT INTO trades(tr_vol,tr_price, tr_ord_id) VALUES ('$volume','$maxprice','$ord_id')";
//THE INSERT EXECUTION
if($this->connection->query($insert_sql)==TRUE){
echo "2.SUCCESSFULL SELL INSERTION \r\n";
}
else{
echo "ERROR SELL INSERTION: ".$this -> connection->error."\r\n";
}
//ADDITION QUERY
$add_sql = "UPDATE market_table SET volume = (volume+$volume) WHERE e_name LIKE '$comp'";
//ADDITION EXECUTION
if($this -> connection->query($add_sql)==TRUE){
echo "3. SUCCESSFULL SELL ADDITION \r\n";
}
else{
echo "ERROR SELL ADDITION: ".$this -> connection->error."\r\n";
}
//PAYMENT QUERY
$total= ($maxprice*$volume);
$pay_sql= "UPDATE sh_credit SET credit = (credit+$total) WHERE sh_id = $sh_id";
//PAYMENT EXECUTION
if($this ->connection ->query($pay_sql)==TRUE){
echo"4. SUCCESSFULL SELL PAYMENT \r\n";
echo"________________________________________________________________\r\n";
}
else{
echo "ERROR SELL PAYMENT: ".$this -> connection->error."\r\n";
}
//BUY ORDER
case 2:
//check if there are stocks available for the order to be executed
$check_sql= "SELECT volume FROM market_table WHERE lower(e_name) LIKE '$comp'";
echo "*********VOLUME CHECK POINT******* \r\n";
$check_result = $this -> connection->query($check_sql);
if($check_result->num_rows > 0){
if($check_row= $check_result->fetch_assoc()){
$check_vol=$check_row["volume"];
echo "remaining volume= ".$check_vol."\r\n";
//execute the order when stocks are available
if($check_vol>0){
$total= ($maxprice*$volume);
//check if shareholder has enough credit
$check_credit_sql="SELECT credit FROM sh_credit WHERE sh_id = $sh_id";
$check_credit_result = $this->connection->query($check_credit_sql);
if ($check_credit_row=$check_credit_result->fetch_assoc()){
$check_credit=$check_credit_row["credit"];
}
//execute the order when credit is enough
if ($check_credit >0){
//charging query
$charge_sql= "UPDATE sh_credit SET credit = (credit-$total) WHERE sh_id = $sh_id";
//charge execution
if($this ->connection ->query($charge_sql)==TRUE){
echo"4. SUCCESSFULL BUY CHARGING \r\n";
}
else{
echo "ERROR BUY CHARGING: ".$this -> connection->error."\r\n";
}
//the update query: update order's status
$update_sql = "UPDATE orders SET status_id = 2 WHERE ord_id = $ord_id";
//update execution
if ($this -> connection->query($update_sql) === TRUE) {
echo "1. SUCCESSFULL BUY UPDATE \r\n";
}
else {
echo "ERROR BUY UPDATE: " . $this -> connection->error."\r\n";
}
//insert query: insert into trades table the executed order
$insert_sql = "INSERT INTO trades(tr_vol,tr_price, tr_ord_id) VALUES ('$volume','$maxprice','$ord_id')";
//insert execution
if($this -> connection->query($insert_sql) == TRUE){
echo "2. SUCCESSFULL BUY INSERTION\r\n";
}
else{
echo "ERROR BUY INSERTION: ".$this -> connection->error."\r\n";
}
//substraction the volume query
$sub_sql = "UPDATE market_table SET volume = (volume-$volume) WHERE e_name LIKE '$comp'";
//sub execution
if($this -> connection->query($sub_sql)==TRUE){
echo "3. SUCCESSFULL BUY SUBSTRACTION \r\n";
echo"________________________________________________________________ \r\n";
}
else{
echo "ERROR BUY SUBSTRACTING: ".$this -> connection->error."\r\n";
}
}
else {
echo"shareholder doesn't have enough credit \r\n";
//cancel query
$cancel_sql="UPDATE orders SET status_id = 3 WHERE ord_id = $ord_id";
//cancel execution
if($this -> connection->query($cancel_sql)==TRUE){
echo"1. ORDER CANCELED SUCCESSFULLY \r\n";
echo "________________________________________________________________ \r\n";
}
else{
echo"ERROR CANCELING ORDER WHEN CREDIT ISN'T ENOUGH \r\n";
}
}
}
else {
echo "available stocks aren't enough \r\n";
//cancel query
$cancel_sql="UPDATE orders SET status_id = 3 WHERE ord_id = $ord_id";
//cancel execution
if($this -> connection->query($cancel_sql)==TRUE){
echo"1. ORDER CANCELED BECAUSE STOCKS ARE ENOUGH \r\n";
echo"________________________________________________________________ \r\n";
}
else{
echo"ERROR CANCELING ORDER WHEN STOCKS AREN'T ENOUGH \r\n";
}
}
}
}
}
}
}
else {
echo "0 results";
}
}
答案 0 :(得分:2)
我认为你应该补充:
break;
在交换机声明中每个案例的末尾。