简体   繁体   中英

Trouble With Inner Join MySQL Query

I have the following MySQL Inner Join query and HTML table.

Table 1: daily_info Table 2: stocks

The join is performed on a column called "Symbol" which is present in both tables. Unfortunately, no data is being generated in the HTML table below. 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,...

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

Where you need to loop on the $result resource -> $result = mysqli_query( $link, $query );

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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