Question

Okay so I am new to PHP and attempting to make a script that takes all of the data from a mysql database I have of stock prices and then looks to see if there was an increase in the stock price during after hours trading (by comparing one day's close price with the next day's open price). I have set up a few scripts like this that work, but for some reason this script isn't copying the data into my sql database and I am completely stumped as to why it won't.When I set up echo statements throughout I discovered that my code is seemingly being executed everywhere but the data isn't transferring. Any help is greatly appreciated.

<?php
include("../includes/connect.php");

function masterLoop(){
$mainTickerFile = fopen("../tickerMaster.txt","r");
while (!feof($mainTickerFile)){
    $companyTicker = fgets($mainTickerFile);
    $companyTicker = trim($companyTicker);

    $nextDayIncrease = 0;
    $nextDayDecrease = 0;
    $nextDayNoChange = 0;
    $total = 0;
    $overnight_change = 0;
    $overnight_change_pct = 0;

    $sumOfIncreases = 0;
    $sumOfDecreases = 0;

    $sql = "SELECT date, open, close, percent_change FROM $companyTicker";
    $result = mysql_query($sql);


    if($result){
        while($row = mysql_fetch_array($result)){
            $date = $row['date'];
            $percent_change = $row['percent_change'];
            $open = $row['open'];
            $close = $row['close'];
            $sql2 = "SELECT date, open, close, percent_change FROM $companyTicker WHERE date > '$date' ORDER BY date ASC LIMIT 1";
            $result2 = mysql_query($sql2);
            $numberOfRows = mysql_num_rows($result2);


            if($numberOfRows==1){
                $row2 = mysql_fetch_row($result2);
                $tom_date= $row2[0];
                $tom_open= $row2[1];
                $tom_close= $row2[2];
                $tom_percent_change= $row2[3];
                if ($close == 0){
                    $close = $tom_open;
                    }
                $overnight_change = $tom_open - $close;
                $overnight_change_pct = ($overnight_change/$close)*100;


                if($overnight_change_pct > 0){
                    $nextDayIncrease++;
                    $sumOfIncreases += $tom_percent_change;
                    $total++;

                }else if($overnight_change_pct < 0){
                    $nextDayDecrease++;
                    $sumOfDecreases += $tom_percent_change;
                    $total++;
                }else{
                    $nextDayNoChange++;
                    $total++;
                }

            }else if ($numberOfRows==0){
                $total = 1;
                $nextDayIncrease = 1;
                $nextDayDecrease = 1;
            }else{
                echo "You have an error in analysis_c3";
            }
        }
    }else{
        echo "unable to select $companyTicker <br />";
    }
    $nextDayIncreasePercent = ($nextDayIncrease/$total) * 100;
    $nextDayDecreasePercent = ($nextDayDecrease/$total) * 100;
    $averageIncreasePercentage = $sumOfIncreases/$nextDayIncrease;
    $averageDecreasePercentage = $sumOfDecreases/$nextDayDecrease;
    insertIntoResultTable($companyTicker, $nextDayIncrease, $nextDayIncreasePercent, $averageIncreasePercentage, $nextDayDecrease, $nextDayDecreasePercent, $averageDecreasePercentage);
}
}


function insertIntoResultTable($companyTicker, $nextDayIncrease, $nextDayIncreasePercent,     $averageIncreasePercentage, $nextDayDecrease, $nextDayDecreasePercent, $averageDecreasePercentage){

$buyValue = $nextDayIncreasePercent * $averageIncreasePercentage;
$sellValue = $nextDayDecreasePercent * $averageDecreasePercentage;
    $trueValue = $buyValue + $sellValue;

$query="SELECT * FROM analysisOvernightGain5 WHERE ticker='$companyTicker' ";
$result=mysql_query($query);
$numberOfRows = mysql_num_rows($result);

if($numberOfRows==1){
    $sql = "UPDATE analysisOvernightGain5 SET ticker='$companyTicker',daysInc='$nextDayIncrease',pctOfDaysInc='$nextDayIncreasePercent',avgIncPct='$averageIncreasePercentage',daysDec='$nextDayDecrease',pctOfDaysDec='$nextDayDecreasePercent',avgDecPct='$averageDecreasePercentage',buyValue='$buyValue',sellValue='$sellValue'trueValue='$trueValue' WHERE ticker='$companyTicker' ";
    mysql_query($sql);
}else{
    $sql="INSERT INTO analysisOvernightGain5 (ticker,daysInc,pctOfDaysInc,avgIncPct,daysDec,pctOfDaysDec,avgDecPct,buyValue,sellValue,trueValue) VALUES ('$companyTicker', '$nextDayIncrease', '$nextDayIncreasePercent', '$averageIncreasePercentage', '$nextDayDecrease', '$nextDayDecreasePercent', '$averageDecreasePercentage', '$buyValue', '$sellValue','$trueValue')";
    mysql_query($sql);
}
}

masterLoop();
?>
Was it helpful?

Solution

you have missed , at ,sellValue='$sellValue'trueValue='$trueValue' it should be ,sellValue='$sellValue',trueValue='$trueValue'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top