2014-05-20 23 views
0

尋求顯示使用PHP存儲在MySQL數據庫中的第一條記錄的'post'字段的值。  以下DIV是其中值是要插入:使用PHP在現有div中顯示MySQL值

<div id="insert1"><?php echo($r); ?></div> 

的PHP(注意事項使用MeekroDB):

<?php 
require_once 'meekrodb.2.2.class.php'; 
DB::$user = 'username'; 
DB::$password = 'password'; 
DB::$dbName = 'database'; 
DB::$host = 'hostname'; 
// get all entries from database table Microblog; 
$results = DB::query("SELECT post FROM MicroBlog"); 
foreach ($results as $row) { 
    $id = $row['id']; 
    // loop through each entry - there's 3 
    for($i = 0; $i < 3; $i++) { 
    // if it's the 1st entry (id = 1) then get field 'post' 
    if ($id == 1) { 
     $r = $row['post']; 
    } 
    } 
} 
?> 

目前#insert1是空白。  我需要更改哪些內容才能進入div?請注意,我想保留for循環,因爲一旦我運行它,我將添加其他if。謝謝。

+0

至少增加最基本的錯誤檢查 –

回答

1

也許這部分是錯誤的:

// get all entries from database table Microblog; 
$results = DB::query("SELECT post FROM MicroBlog"); 

應該是:

// get all entries from database table Microblog; 
$results = DB::query("SELECT * FROM MicroBlog"); 

你得到的id,我想你不會從查詢得到。

$id = $row['id']; 

其他選項:

註釋掉的$ id。

//$id = $row['id']; 

,並在下面的if塊,改變$ id來$ I:

if ($i == 1) { 
     $r = $row['post']; 
    } 

要添加的記錄數:

$x = 0; 
    foreach ($results as $row) { 
     $x++; 
     $id = $row['id']; 
     // loop through each entry - there's 3 
     for($i = 0; $i < 3; $i++) { 
     // if it's the 1st entry (id = 1) then get field 'post' 
     if ($id == 1) { 
      $r = $row['post']; 
     } 
     } 
    } 
echo $x; 
+0

謝謝,那有效。如果您不介意我問我關於限制$結果的幾個問題:我怎樣才能得到結果中的記錄數?如何限制記錄到只有那些開始瓦特/數字,A和B?如何限制到15條記錄/最近的日期? (從最近列出)Thanks – parti

+1

使用mysql_num_rows()函數,以便$ number_of_records = mysql_num_rows($ result); – kimbarcelona

+0

你能幫我解決其他問題嗎? – parti