内部加入MySQL查询的麻烦

时间:2014-06-21 04:28:06

标签: php mysql sql

我有以下MySQL内部联接查询和HTML表。

表1:daily_info 表2:股票

连接在名为“Symbol”的列上执行,该列存在于两个表中。遗憾的是,下面的HTML表格中没有生成任何数据。我错过了什么?

<?php
$query = "SELECT daily_info.Day, daily_info.Prev_close, stocks.Symbol, stocks.Company, stocks.Description FROM stocks INNER JOIN daily_info ON stocks.Symbol = daily_info.Symbol ORDER BY Day Desc"; 

$result = mysqli_query( $link, $query );


// All good?
if ( !$result ) {
  // Nope
  $message  = 'Invalid query: ' . mysql_error() . "\n";
  $message .= 'Whole query: ' . $query;
  die( $message );
}

?>
<br />
<hr />
<br />
<div id="table-wrapper">
<div id="table-scroll">
<table width="100%" style="text-align:center; vertical-align:middle'">
<thead><tr>
  <th><span class="text">Company</span></th>
  <th><span class="text">Symbol</span></th>
  <th><span class="text">Previous Close</span></th>
</tr></thead>
<?php
while ( $row = mysqli_fetch_assoc($query) ) {
  echo "<tr>";
  echo "<td><a href=\"http://finance.yahoo.com/q?s=" . $row['Symbol'] . "\" target=\"_blank\">" . $row['Company'] . "</a></td>";
  echo "<td><a href=\"http://finance.yahoo.com/q?s=" . $row['Symbol'] . "\" target=\"_blank\">" . $row['Symbol'] . "</a></td>";
  echo "<td>" . $row['Prev_close'] . "</td>";
  echo "</tr>";
}
?>
</table>

1 个答案:

答案 0 :(得分:1)

您正在循环$query字符串 - &gt; $query = "SELECT daily_info.Day,...

<?php
while ( $row = mysqli_fetch_assoc($query) ) {
                                  ^^^^^^

您需要在$result资源上循环的地方 - &gt; $result = mysqli_query( $link, $query );

<?php
while ( $row = mysqli_fetch_assoc($result) ) {
                                  ^^^^^^^