2012-02-26 46 views
-2

當我嘗試輸入一個ID字段來標記我的各個帖子(post-id:1932等)時,該值總是打印爲0,而不是數據庫的值。在int值中打印0的PHP/MySQL

該查詢需要$ _REQUEST的主題變量(通過單擊鏈接生成)。

這裏是我的代碼:

<?php 

require_once 'connect.php'; 

$topic = $_REQUEST['topic']; 

// Build the SELECT statement 
$select_posts = 
"SELECT p.topic, p.title, p.pic_id, p.snippet, p.content, p.author_id, p.date FROM posts p WHERE p.topic = '" . $topic . "' OR p.topic='Admin' ORDER BY p.date DESC"; 

// Run the Query 
$result = mysql_query($select_posts); 

?> 

<?php 
    $post_count=0; 
    while ($post_count<10 && $posts = mysql_fetch_array($result)) { 
     $posts_row = sprintf(
      '<entry> 
       <headline>%s</headline> 
        <pic><img class=inlinepic src=/Sustenance/assets/post-pics/%d.jpg></pic> 
        <snippet class=on id="%d snippet">%s</snippet> 
        <readmore id="readmore%d" class=on onclick=readmore("%d")>Read More</readmore> 
        <content id="%d" class="less">%s 
         <readmore class=on onclick=readless(%d)>Show Less</readmore> 
        </content> 
        <byline> 
         <by>by</by> 
         <author>%s</author> 
        </byline> 
      </entry>', 
     $posts['topic'], $posts['title'], $posts['pic_id'], $posts['post_id'], $posts['snippet'], $posts['post_id'], $posts['post_id'], $posts['post_id'], $posts['content'], $posts['post_id'], $posts['author_id']); 
     echo $posts_row; 
     $post_count++; 
    } 
?> 

我感到奇怪的是「pic_id」正確打印,並以正確的圖片拉,但即使當我試圖重用pic_id在$ DIVID它不」工作。我嘗試使用$ posts ['id']替換$ divid,使用PHP Manual中的所有不同的整數類型說明符,甚至使用內聯php。

我有語法錯誤嗎?爲什麼它保持打印0?

編輯:我已經發布查詢按要求

編輯2:刪除了$ DIVID變量。

+0

請發佈查詢。 – 2012-02-26 23:18:28

+1

不應該* $ divid = $ posts ['post_id']; * be ininside_ while循環? – 2012-02-26 23:19:34

回答

0

你必須放線

$divid=$posts['post_id']; 

while循環內。現在它試圖在循環頭中第一次分配$posts['post_id']之前獲得值,因此是0,並且永遠不會改變(因爲它不會被重新分配)。

+0

這是一個很好的觀點!我把它移回到它應該在的地方。但是,這似乎並沒有解決問題。 – dantiston 2012-02-26 23:52:30