2016-06-26 85 views
0
<?php 

    $koneksi = mysqli_connect('mysql.idhostinger.com','root','pw','db'); 
    $result = mysqli_query($koneksi,'select * from posting'); 

    while($row = mysqli_fetch_row($result)) { 

    for($x = 0; $x <= count($row); $x++) { 
     echo "<div style='background:green'>$row[$x]</div>"; 
    } 
    } 
?> 

Hello guys!這段代碼從mysql獲取數據,然後將其打印到div容器中,並用div包裝。從mysql數據庫提取的打印數據

問題是捕獲的每行數據產生2格。

如何讓每一行產生只有1格?

謝謝。

+0

行包含2個值作爲查詢的結果。寫'從...選擇one_field' – splash58

+1

你能告訴我們一些關於張貼表的結構嗎?你的代碼爲每一行的每一列產生一個新的div。 – zajonc

+0

該表每行只有1列 –

回答

1

你每行中創建一個div每個項目

while($row = mysqli_fetch_row($result)) { 
    for($x=0;$x<=count($row);$x++){ 
     echo "<div style='background:green'>$row[$x]</div>"; 
    } 
} 

如果你想爲「行」的單一div,並把所有的項目在div,放在div環路,超過行迭代:

while($row = mysqli_fetch_row($result)) { 
    echo "<div style='background:green'>"; 
    for($x=0;$x<=count($row);$x++){ 
     echo $row[$x]; 
    } 
    echo "</div>"; 
} 
1

echo線由while和執行0循環,你只需echo它在while循環中很好地幫助你。

感謝

<?php 
$koneksi = mysqli_connect('mysql.idhostinger.com','root','pw','db'); 
$result = mysqli_query($koneksi,'select * from posting'); 
while($row = mysqli_fetch_row($result)){ 
$div = "<div style='background:green'>"; 
for($x=0;$x<=count($row);$x++){ 
$div .= "<p>$row[$x]</p>"; 
} 
$div .= "</div>"; 
echo $div; 
} 
?> 
0
for($x=0;$x<=2;$x++){ 
echo "<div style='background:green'>$x</div></br>"; 
} 

每一個數字都格..

如果u希望把所有的數一個DIV:

<div style='background:green'> 
<?php 
for($x=0;$x<=2;$x++){ 
echo $x; 
} 
?> 
</div> 

或者如果u想拉數據從其他方式像js,只需從隱藏的div中獲取文本並拉到「#div」.html ()或.text()

0

儘管您只是在簡單地使用SELECT * FROM TABLE_NAME,但仍然建議您使用PDO的原因有很多,您可能會親自研究這些原因。牢記這一方法;這裏是可以在這方面幫助:

<?php 
     // USING PDO HAS SO MUCH ADVANTAGES OVER mysqli + mysql EVEN IN SIMPLE CASES LIKE SELECT * 

     $HOST  = "mysql.idhostinger.com";  // CHANGE TO YOUR HOST 
     $USER  = "root";      // CHANGE TO YOUR USERNAME 
     $PASS  = "pw";       // CHANGE TO YOUR PASSWORD 
     $D_BASE  = "db";       // CHANGE TO YOUR DATABASE NAME 

     try { 
      $dbh   = new PDO("mysql:host=$HOST;dbname=$D_BASE", $USER, $PASS); 
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

      $stmt   = $dbh->prepare("SELECT * FROM posting"); 
      $status   = $stmt->execute(); 
      $results  = $stmt->fetchAll(PDO::FETCH_OBJ); 
      $arrData  = array(); 
      $strOutput  = "<div class=''>" . PHP_EOL; //<== AN OUTER DIV TO HOLD ALL CHILD DIVS: THE ONES CONTAINING THE ROW-DATA. 

      if($results){ 
       foreach($results as $intKey=>$result){ 
        // BUILD THE OUTPUT IN THE FOREACH LOOP USING A VARIABLE 
        // AND THE COLUMN NAME... 
        // HERE $columnName IS THE SAME AS THE NAME OF YOUR 
        // TARGET COLUMN IN THE DB-TABLE (EG: `post_content`) 
        // THAT IS: $result->post_content ;-) 
        $strOutput .= "<div style='background:green'>{$result->columnName}</div>" . PHP_EOL; 
       } 
       $strOutput .= "</div>" . PHP_EOL;   //<== CLOSE THE OUTER DIV 
      } 
      // YOU CAN NOW ECHO THE OUTPUT ANYWHERE.... MUCH MORE FLEXIBLE, HUH? 
      echo $strOutput; 

      //GARBAGE COLLECTION 
      $dbh  = null; 

     }catch(PDOException $e){ 
      echo $e->getMessage(); 
     } 

這PDO方法(在這種情況下)可能會出現冗長了很多(和它似乎值得商榷的那樣),但是,從長遠來看,它確實是值得的.. 。