1

我的查询很适合我正在尝试做的事情,但我想扩展它并从 stockpicks 表中获取所有其他信息。它现在所做的是匹配用户搜索股票代码,然后查询我的数据库以查看是否有匹配项,但不允许使用该表中的其他数据。这是我在更改之前所拥有的:

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

这就是我一直在尝试做的事情和我的问题。

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

我收到此错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'as sym WHERE symbol LIKE '%hig%')' 附近使用正确的语法

正如我之前所说,我想简单地调用所有信息以防万一,而不是仅仅调用符号。我不想将其他信息与搜索匹配,但我希望能够结合匹配使用其他数据进行输出。如果这没有意义,请告诉我。感谢您提供的所有有用信息。其无价之宝。克里斯

编辑:表结构是

编号 | 符号 | 条目 | 退出 | 开闭 | 入境日期

为了更好地理解,我将包含整个脚本。如果它是一个未平仓头寸,我试图让变量可用。

<?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='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>";

if (isset($_POST['get_quote']) && $_POST['get_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%')";

$sym = strtoupper($sym);
$get_quote = strtoupper($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 .= "
<div class='rightOfStocks'><hr />It Looks like <strong>'$get_quote'</strong> is one of our stock picks<hr />


</div>";
while($row = mysql_fetch_array($query)){
            $id = $row["id"];

   $sym = $row["sym"];
   $sym = strtoupper($sym);
   $search_output .= "

   <div class='rightOfStocks'><h3>Our Pick</h3>$sym<br />
   <br />$$quote_1
   </div>";
                } // close while
} else {
$search_output = "
<div class='rightOfStocks'><hr />0 results for <strong>$get_quote</strong>


<hr /></div>";
}
}

?>                              
                                    <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" />
                                        Enter any valid stock quote (ie. AAPL HOG RIMM):<br></form></div>


                                        <!--left of stock results close--></div><!--left of stock results close-->
                                        <?php echo $search_output ;?>
                                </div>
4

2 回答 2

3
 SELECT * FROM stockpicks symbol as sym WHERE symbol LIKE '%$get_quote%'

应该

 SELECT * FROM stockpicks as sym WHERE symbol LIKE '%$get_quote%'

通常,您可以通过查看错误中指出的语法之前来查找 mysql 错误。在这种情况下,"FROM stockpicks symbol"将表分配给stockpicks化名"symbol",因此以下"AS sym"语法错误。考虑运算符"AS"在此分配中是可选的。"FROM stockpicks symbol"意思是一样的"FROM stockpicks AS symbol"

应该注意,在这种情况下分配别名是没有意义的,因为它没有被使用。但是分配是错误的原因。

于 2013-07-03T18:18:20.050 回答
1

使用别名时,“as”关键字是可选的。所以它认为“symbol”是“stockpicks”的别名,但之后它不知道该做什么。

如果它可以按原样返回符号字段,那么您只需要省略该部分:

$sqlCommand = "(SELECT * FROM stockpicks WHERE symbol LIKE '%$get_quote%')";

如果您确实需要将字段符号作为 sym 返回,那么这应该可以:

$sqlCommand = "(SELECT *, symbol as sym FROM stockpicks WHERE symbol LIKE '%$get_quote%')";
于 2013-07-03T18:24:13.830 回答