PHP MYSQL匹配和输出

时间:2013-07-03 04:03:09

标签: php mysql match

我很难弄清楚我在这里做了什么。让我先解释一下我要做的事情以及我到目前为止所做的事情。

我有什么:

到目前为止,我已经拥有了从雅虎获取数据以填充搜索范围的方式。这是一个简单的表单,要求访问者输入一个符号,然后在其中吐出引用信息。

我的目标:

我试图关联我的数据库以显示我的数据库信息,如果股票代码是我有信息的话。所以说访客输入股票代码" hig"在表格中,我想打电话给我的桌子' stockpicks'查看列'符号'中是否有匹配项。如果是这种情况,我希望从其他列输出该表中的其他特定数据,例如' notes'例如。

以下是有效样本:http://www.stocksandstocks.com/stock-quotes.php 我似乎无法弄清楚两者之间的关系以及如何做到正确。以下是我到目前为止的情况。

    <?php
error_reporting(E_ALL ^ E_NOTICE); //this is for debugging, remove if you dont need anymore
ini_set("display_errors", 1); //this is for debugging, remove if you dont need anymore
$searchoutput = "";
$ticker = "goog";

if (isset($_POST['get_quote'])) {
$ticker = $_POST['ticker'];
}

$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&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='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>";

if (isset($_POST['get_quote']) && $_POST['get_quote'] != "") {
$ticker = $_POST['ticker'];
    $get_quote = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['get_quote']);

$sqlCommand = "(SELECT id, symbol as sym FROM stockpicks WHERE symbol LIKE '%$get_quote%')";

        include_once("storescripts/connect_to_mysql.php");
        $query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$get_quote/strong><hr />$sqlCommand<hr />";
while($row = mysql_fetch_array($query)){
            $id = $row["id"];
   $sym = $row["sym"];
   $search_output .= "Item ID: $id - $sym<br />";
                } // close while
} else {
$search_output = "<hr />0 results for <strong>$get_quote</strong><hr />$sqlCommand";
}
}

?>                              
                                    <div class="form">  <form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>">
                                        Get Quote: <input type="text" size="10" maxlength="10" name="ticker"/>
                                        <input type="submit" value="Get Quote" name="get_quote" />
                                        </form></div>
                                        Enter any valid stock quote such as:<br>
                                        aapl<br>
                                        hog<br>
                                        rimm<br>
                                        rht<br>
                                        <?php echo $search_output ;?>
                                </div>

1 个答案:

答案 0 :(得分:1)

您需要在if($count > 1){更改条件以阅读if($count >= 1){,然后您就可以读取查询返回的行。

你遇到的第二个问题是标记变量的错过。您需要使用ticker的值,现在使用“qet_quote”这是一个按钮的名称。

$ticker = $_POST['ticker'];
$get_quote = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['ticker']);

$sqlCommand = "(SELECT id, symbol as sym FROM stockpicks WHERE symbol LIKE '%$get_quote%')";