简体   繁体   中英

join mySQL query, two of the same column names, how to display data?

I have created my first join query to display some Wordpress content on a home page of a website, so far so good, but there are two columns in the joined tables with name "ID". I lost ability to use one of the column names as a variable later on (in a link).

"echo $row['ID']" does not display the value anymore (used to, before tables were joined).

"echo $row['wp_posts.ID']" does not seem to do anything either.

What should I do?

$query = "SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_excerpt, wp_posts.post_author, wp_users.ID, wp_users.display_name FROM wp_posts, wp_users  WHERE wp_posts.post_status = 'publish' AND wp_users.ID = wp_posts.post_author ORDER BY wp_posts.post_date DESC LIMIT 2";
$result = mysql_query($query) or die(mysql_error()); 
?>

<?php 
      while ($row = mysql_fetch_array($result)){ 
          echo "<h2>" . $row['post_title']; ?></h2>
          <p class="post_author">
          by <?php echo $row['display_name'];?></p>
          <?php $text = explode("***",wordwrap(strip_tags($row['post_excerpt']),150,"***",true));
                echo " ".$text[0]." ";
        ?>... <a href="http://www.pihl.ca/kelownalawyers/?p=<?php echo $row['ID'];?>"> READ MORE</a><p />

<?php 

}  
?>
SELECT wp_posts.ID AS wpp_id, wp_users.ID AS wpu_id

然后在循环中使用$ row ['wpp_id']和$ row ['wpu_id']进行引用

If you want to select all but still resolve the naming conflict with certain fields, this will work too:

SELECT *, wp_posts.ID AS wpp_id, wp_users.ID AS wpu_id FROM...

This allows you to grab all column, with the addition of 2 new columns which can then be grabbed from the assoc array.

使用MySQL AS关键字为ID字段之一提供别名( MySQL SELECT Docs

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