我的查询对我正在尝试做的事情很有效,但我想扩展它并从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服务器版本,以便使用正确的语法 靠近'as sym WHERE symbol LIKE'%hig%')'在第1行
正如我之前所说的,我想简单地调用所有信息,以防需要而不是仅仅调用符号。我不想将其他信息与搜索匹配,但我希望能够将其他数据与匹配结合使用。如果这没有意义,请告诉我。感谢您提供的所有有用信息。它非常宝贵。 克里斯
编辑:表格结构
id |符号|进入|退出| openclosed | entrydate
为了更好地理解,我将包括整个脚本。我试图让变量可用,如果它是一个空位。
<?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>
答案 0 :(得分: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"
应该注意的是,在这种情况下,别名的分配是没有意义的,因为它没有被使用。但是分配是错误的原因。
答案 1 :(得分:1)
使用别名时,“as”关键字是可选的。因此它认为“符号”是“stockpicks”的别名,但之后它不知道该怎么做。
如果它可以返回名为as的符号字段,那么你只需要省略该部分:
$sqlCommand = "(SELECT * FROM stockpicks WHERE symbol LIKE '%$get_quote%')";
如果您确实需要将字段符号作为sym返回,那么这应该有效:
$sqlCommand = "(SELECT *, symbol as sym FROM stockpicks WHERE symbol LIKE '%$get_quote%')";