使用数组抓取所有MYSQL行

时间:2013-07-08 17:19:58

标签: php mysql arrays

您好我正在尝试输出表格中的所有行和信息:

ID,符号,项,exit,openclosed,entrydate,longshort,target_one,target_two,target_three,笔记

这是通过这个脚本我正在努力获得这个功能。现在我只输出一个数据库条目。这个条目当然是最后一个。作为参考,最后的输入符号是GLD。我希望继续使用下一个符号,但似乎无法将其输出。 quote_0,quote_1等的输出数据。来自雅虎作为阵列。

<?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 个答案:

答案 0 :(得分:2)

您必须将输出更多地输入到while循环中才能访问表中的每个值。

<?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

?> 

答案 1 :(得分:0)

  1. 你应该开始使用msqli函数而不是mysql sinc eit已被弃用。
  2. 如果您的代码中的其他所有内容都正确,则您需要做的唯一更改是在while子句中。

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