0

Hi I'm trying to output all of the rows and information from my table :

id,Symbol,entry,exit,openclosed,entrydate,longshort,target_one,target_two,target_three,notes

This is through this script I'm working on to get this functionality. Right now I'm only outputting one of the database entries. This entry of course being the last one. For reference the last entry symbol is GLD. I'd like it to continue with the next symbols, but can't seem to get it to output. The outputted data for quote_0,quote_1 ect. come from yahoo as an array.

<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
//begin
    include "storescripts/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT * FROM stockpicks ORDER BY id LIMIT 100");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        // get all the product details
        while($row = mysql_fetch_array($sql)){ 
            $id = $row["id"];
            $symbol = $row["symbol"];
         }
    }
    mysql_close();
    //end
        if(empty($symbol)) {
            echo nothing;
        }
            else {
                $open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv", "r");
                $quote = fread($open, 1000);

                fclose($open);

                $quote = str_replace("\"", "", $quote);
                $quote = explode(",", $quote);

                $quote_0 = ($quote[0]);
                $quote_1 = ($quote[1]);
                $quote_2 = ($quote[2]);
                $quote_3 = ($quote[3]);
                $quote_4 = ($quote[4]);
                $quote_5 = ($quote[5]);
                $quote_6 = ($quote[6]);
                $quote_7 = ($quote[7]);
                $quote_8 = ($quote[8]);

                echo "<div class='symbol'><div class='quote'>Company: $quote_0</div></div>";
                echo "<div class='leftofStocks'><div class='row'><div class='quote'>Last trade: $$quote_1</div>";
                echo "<div class='quote'>Date: $quote_2</div>";
                echo "<div class='quote'>Time: $quote_3</div>";
                echo "<div class='quote'>From Previous: $$quote_4</div></div>";
                echo "<div class='row'><div class='quote'>Open: $$quote_5</div>";
                echo "<div class='quote'>High: $$quote_6</div>";
                echo "<div class='quote'>Low: $$quote_7</div>";
                echo "<div class='quote'>Volume: $quote_8</div></div>";
}
?>          
2
  • Mysql is no longer used please use mysqli or pdo Commented Jul 8, 2013 at 17:27
  • To clarify; php's mysql_ methods are deprecated and may no longer be supported in future versions of PHP. Use PDO or mysqli_ methods instead. Commented Jul 8, 2013 at 17:33

2 Answers 2

2

You have to more your output into the while loop to get access to each of the values in your table.

<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1);
//begin
    include "storescripts/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT * FROM stockpicks ORDER BY id LIMIT 100");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        // get all the product details
        while($row = mysql_fetch_array($sql)){ 
            $id = $row["id"];
            $symbol = $row["symbol"];
            if(empty($symbol)) {
                echo nothing;
            }
                else {
                    $open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv", "r");
                    $quote = fread($open, 1000);

                    fclose($open);

                    $quote = str_replace("\"", "", $quote);
                    $quote = explode(",", $quote);

                    $quote_0 = ($quote[0]);
                    $quote_1 = ($quote[1]);
                    $quote_2 = ($quote[2]);
                    $quote_3 = ($quote[3]);
                    $quote_4 = ($quote[4]);
                    $quote_5 = ($quote[5]);
                    $quote_6 = ($quote[6]);
                    $quote_7 = ($quote[7]);
                    $quote_8 = ($quote[8]);

                    echo "<div class='symbol'><div class='quote'>Company: $quote_0</div></div>";
                    echo "<div class='leftofStocks'><div class='row'><div class='quote'>Last trade: $$quote_1</div>";
                    echo "<div class='quote'>Date: $quote_2</div>";
                    echo "<div class='quote'>Time: $quote_3</div>";
                    echo "<div class='quote'>From Previous: $$quote_4</div></div>";
                    echo "<div class='row'><div class='quote'>Open: $$quote_5</div>";
                    echo "<div class='quote'>High: $$quote_6</div>";
                    echo "<div class='quote'>Low: $$quote_7</div>";
                    echo "<div class='quote'>Volume: $quote_8</div></div>";
            }
         }
    }
    mysql_close();
    //end

?> 
Sign up to request clarification or add additional context in comments.

3 Comments

Hi orangepill. That worked perfectly, but can you let me know exactly what was going on for future reference.
You where reading each value from the array and reassigning it to the $id and $symbol variables but you weren't using those values until you where done walking the whole record set so when you got to the output phase $id and $symbol only reflected the very last values that where assigned in the while loop.
fantastic orangepill. Thanks for that reference. I will be bookmarking this answer. Thanks again!
0
  1. you should start using the msqli functions instead of mysql sinc eit is deprecated.
  2. if everything else is correct in your code the only change you need to do is in the while clause.

    while ($row = mysql_fetch_assoc($sql)){
    $id[]=$row['id'];
    $symbol[] =$row['symbol'];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.