简体   繁体   English

内部联接MySQL查询出现问题

[英]Trouble With Inner Join MySQL Query

I have the following MySQL Inner Join query and HTML table. 我有以下MySQL内部连接查询和HTML表。

Table 1: daily_info Table 2: stocks 表1:daily_info表2:库存

The join is performed on a column called "Symbol" which is present in both tables. 联接是在两个表中都存在的名为“ Symbol”的列上执行的。 Unfortunately, no data is being generated in the HTML table below. 不幸的是,下面的HTML表中没有生成任何数据。 What am I missing? 我想念什么?

<?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>

You are looping on the $query string -> $query = "SELECT daily_info.Day,... 您正在循环$query字符串-> $query = "SELECT daily_info.Day,...

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

Where you need to loop on the $result resource -> $result = mysqli_query( $link, $query ); 您需要在$result资源上循环的地方-> $result = mysqli_query( $link, $query );

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM