2017-06-20 64 views
0

我想一個PHP編寫的語句的結果保存到一個數組,然後寫出來(回聲)PHP編寫的語句將結果放入數組

<?php 

require_once "DBinit.php"; 

$vpisna=$_POST['vpisna']; 

$dbh = DBInit::getInstance(); 



$stmt = $dbh->prepare("SELECT * FROM studenti"); 


$stmt->bindValue(':id_student', $id_student); 
$stmt->bindValue(':ime', $ime); 
$stmt->bindValue(':priimek', $priimek); 
$stmt->bindValue(':naslov', $naslov); 
$stmt->bindValue(':email', $email); 
$stmt->bindValue(':vpisna', $vpisna); 
$stmt->bindValue(':geslo', $geslo); 
$stmt->bindValue(':status', $status); 

$stmt->execute(); 


     $result = $stmt->fetch(PDO::FETCH_ASSOC); 


     //$result = mysqli_query($con,"SELECT * FROM studenti"); 

     echo "<table border='1' width='100%'> 
     <tr> 
     <th> Št.</th> 
     <th>Ime</th> 
     <th>Priimek</th> 
     <th>Naslov</th> 
     <th>Email</th> 
     <th>Vpisna številka</th> 
     <th>Geslo</th> 
     <th>Status</th> 
     </tr>"; 

     while($row = mysqli_fetch_array($result)) { 
      echo "<tr>"; 
      echo "<td>" . $row['id_student'] . "</td>"; 
      echo "<td>" . $row['ime'] . "</td>"; 
      echo "<td>" . $row['priimek'] . "</td>"; 
      echo "<td>" . $row['naslov'] . "</td>"; 
      echo "<td>" . $row['email'] . "</td>"; 
      echo "<td>" . $row['vpisna'] . "</td>"; 
      echo "<td>" . $row['geslo'] . "</td>"; 
      echo "<td>" . $row['status'] . "</td>"; 

      echo "</tr>"; 
     } 

     echo "</table>"; 


?> 

謝謝你的幫助。

這應該寫出(在瀏覽器中)與他們的信息的學生列表。

我無法弄清楚如何將stmt結果保存到數組中以使用$ row讀取它。

+1

你似乎是混合PDO和mysqli – RamRaider

+0

您還將值綁定到沒有佔位符的語句。 –

+0

PDO有一個'fetchAll'方法〜'PDOStatement :: fetchAll' - 使用該方案 – RamRaider

回答

0

起初,你必須插入新行用INSERT語句...

$row = [ 
    'ime' => 'Bob', 
    'email' => '[email protected]' 
]; 
$sql = "INSERT INTO studenti SET ime=:ime, email=:email"; 
$dbh->prepare($sql)->execute($row); 

...那麼你可以選擇輸出的所有行:

$stmt = $dbh->query('SELECT * FROM studenti'); 
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    echo "<tr>"; 
    echo "<td>" . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . "</td>"; 
    // ... 
    echo "</tr>"; 
}