gpt4 book ai didi

PHP MYSQL 匹配并输出

转载 作者:行者123 更新时间:2023-11-29 13:45:43 25 4
gpt4 key购买 nike

我很难弄清楚我在这里做什么。首先让我解释一下我正在尝试做什么以及到目前为止我得到了什么。

我有:

到目前为止,我已经掌握了从 yahoo 获取数据以填充搜索的方式。这是一个简单的表单,要求访问者输入一个符号,然后在其中吐出报价信息。

我的目标:

如果我有关于股票代码的信息,我正在尝试关联我的数据库以显示我的数据库信息。假设访问者在表格中输入股票代码“hig”,我想将我的表称为“stockpicks”,以查看“symbol”列中是否有匹配项。如果是这种情况,我想从其他列(例如“注释”)输出该表中的其他特定数据。

这是事件示例: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>

最佳答案

您需要在此处更改条件 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%')";

关于PHP MYSQL 匹配并输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17439472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com