Mysqli表加入

时间:2013-12-12 12:14:41

标签: php mysql

我有一个新闻表,并希望将其链接到成员表以使用Left Join获取作者信息,但我无法从我的php语句中获取信息。这是我的PHP代码:

会员表:身份证,类型,用户名,姓名,头像

新闻表:id,类型,标题,文章,海报

<?php
CODE FOR PAGINATION
?>
<?php
if($usertype == "pub"){
    $get_articles_sql = "SELECT news.*, members.avatar, members.type AS membertype, members.name FROM news LEFT JOIN members ON members.id = news.poster LIMIT $offset, $post_limit";
}
else if($filter == "false"){
    $get_articles_sql = "SELECT news.*, members.avatar, members.type AS membertype, members.name FROM news LEFT JOIN members ON members.id = news.poster WHERE 3959 * acos( Cos( RADIANS(lat) ) * Cos( RADIANS('$yourlat') ) * Cos( RADIANS('$yourlon') - RADIANS(lon) ) + Sin( RADIANS(lat) ) * Sin( RADIANS('$yourlat'))) <= $yourradius ORDER BY news.date DESC LIMIT $offset, $post_limit";
}
else{
    $get_articles_sql = "SELECT news.*, members.avatar, members.type AS membertype, members.name FROM news LEFT JOIN members ON members.id = news.poster WHERE 3959 * acos( Cos( RADIANS(lat) ) * Cos( RADIANS('$yourlat') ) * Cos( RADIANS('$yourlon') - RADIANS(lon) ) + Sin( RADIANS(lat) ) * Sin( RADIANS('$yourlat'))) <= $yourradius AND type = '$filter' LIMIT $offset, $post_limit";
}
$get_articles_res = mysqli_query($con, $get_articles_sql);
if($get_articles_res){
    $num_articles = mysqli_num_rows($get_articles_res);
}else{
    $num_articles = "0";
}
?>
<?php
if($num_articles > 0){
while($article = mysqli_fetch_assoc($get_articles_res)){
    $article_id = $article["id"];
    $article_type = $article["type"];
    $article_title = $article["title"];
    $article_article = $article["article"];
    $article_city = $article["city"];
    $article_town = $article["county"];
    $article_date = $article["date"];
    $article_avatar = $article["avatar"];
    $article_name = $article["name"];
    $article_membertype = $article["membertype"];

    $show_article .= "

        <div class=\"section\">
            <div class=\"sectionInner\">
                <div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/$article_avatar\"></div>
                <div class=\"searchInformationLeft\"><div class=\"searchInformationPrimary\">$article_title</div><div class=\"searchInformationSecondary\"><i>&quot;$article_article&quot;</i></div></div>
                <div class=\"searchInformationRight\"><div class=\"searchInformationPrimary\">By $article_name</div><div class=\"searchInformationSecondary\">$article_membertype, $article_city</div><div class=\"searchInformationThird\"><a href=\"event.php?id=$event_id&url=".urlencode($url)."\">View Details</a></div></div>
                <div class=\"clearLeft\"></div>
            </div>
        </div>
        <div class=\"searchResultSplitter\"></div>

    ";
}
}else{...}
?>

我尝试使用“SELECT * FROM new WHERE ...”并且它工作正常,因此连接中必定存在错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

第二个和第三个查询的问题是您尝试在计算中使用 lat lon 列,但是您没有在新闻表或会员表。