2012-12-18 59 views
0

出於某種原因,這是行不通的,但不知道爲什麼。我試圖抓住關於數據庫中有多少評論的評論ID,但它顯示「0」。當我清楚地看到評論。抓文章ID帖子

訪問過:http://www.krissales.me/#/media/articles

如果單擊文章鏈接,它會告訴我要多少意見在該網頁上,而不是在頁面上展示上。

我敢肯定,它有什麼做的:

$ amount_get =請求mysql_query( 「SELECT * FROM評論WHERE ID ='」。 mysql_real_escape_string($ _ GET [ '身份證']) 。「'」); $ comments = mysql_num_rows($ amount_get);

article.php:

<?php 

     $amount_get = mysql_query("SELECT * FROM comment WHERE id='" . mysql_real_escape_string($_GET['id']) . "'"); 
     $comments = mysql_num_rows($amount_get);    

     $grab = mysql_query("SELECT * FROM articles WHERE id='" . mysql_real_escape_string($_GET['id']) . "' LIMIT 1");  
     $grab = mysql_query("SELECT id, news_title, news_content, news_author, news_day, news_month, news_year, news_date FROM articles ORDER BY id DESC limit 3"); 
     $news_day =$row['news_day']; 
     $news_month =$row['news_month']; 
     $news_year =$row['news_year']; 


     if (mysql_num_rows($grab)==0) { 
     echo "<div class='alert alert-note-x'>Sorry, it looks like their are no articles posted!</div>"; 
    } 
     while($row = mysql_fetch_array($grab)){ 

    ?> 

    <div class="post-container"> 
     <div class="post"> 
     <div class="left"> 
     <div class="date"><span class="day"><?php echo $row['news_day'] ?></span> <span class="month"><?php echo $row['news_month'] ?></span> <span class="year"><?php echo $row['news_year'] ?></span></div></div> 
     <div class="postcontent"><h5 class="posttitle"><a href="#/media/<?php echo $row['id'] ?>.<?php echo stripslashes(str_replace(" ", "-", $row['news_title'])); ?>"><?php echo stripslashes($row['news_title']); ?></a></h5>      
      <p>The content for this article will be posted here shortly, i need to edit some PHP that limits the chars for this section</p> 
      <p>Comments (<?php echo $comments ?>)</p> 
      </div> 
    </div>​ 
    </div> 

    <?php } ?> 
+1

您正在通過特定ID進行計數,所以我最多隻能得到1個,否則您的ID意味着其他的東西? –

+0

我認爲你不必封裝id,因爲它是一個數字值。嘗試不帶引號:「SELECT * FROM comment WHERE id =」。 mysql_real_escape_string($ _ GET ['id'])。 「」 – MAXIM

+0

@MAXIM是一個數值,mysql_real_escape_STRING也是無用的。將它轉換爲int,或者檢查它是否已經是 –

回答

2

你應該看看這行的WHERE條件:

$amount_get = mysql_query("SELECT * FROM comment WHERE id='" . mysql_real_escape_string($_GET['id']) . "'"); 

我想這應該id被更改爲類似article_id - 一個foreign_key指向一篇文章。

+0

是的,我試過了。 $ amount_get = mysql_query(「SELECT * FROM comment WHERE articleid ='」。mysql_real_escape_string($ _ GET ['id'])。「'」);但它不起作用。 – TrippedStackers

+0

'articleId'確實是列的名字?什麼意思是「不起作用」?你有錯誤信息嗎? 'mysql_error()'說什麼? (執行查詢後)。你能發佈文章詳細頁面的(相關)php代碼嗎? – hek2mgl

+0

我在任何地方都看不到sql錯誤。這是完整的php http://pastebin.com/fNYSSRWk,是的,articleid是有效的:http://puu.sh/1Cuh8 – TrippedStackers