2017-04-14 115 views
0

我在我的數據庫中有一個名爲sales的表。每行在聯邦列中都有一個值。 所以我想收集在這個代碼中的數組中的所有聯邦,但它只是不工作,我不知道爲什麼...請幫助!謝謝!mysql&php select *查詢不起作用

<?php 
       $db = mysqli_connect("server", "usr", "pw", "db"); 

       $FederalRes = $db->query("SELECT * FROM `sales` WHERE `state` = 'Österreich'"); //gets every row with the state = 'Österreich' 

       $federals = array(); // empty array to collect federals 

       while ($row = $FederalRes->fetch_object()) { 
       if (!in_array($row->federal, $federals)) { // check if federal is already in array 
        array_push($federals, $row->federal); // adds federal to array 
       } 
       } 

       sort($federals); // sorts the array alphabetically 

       foreach ($federals as $key => $value): ?> 
       <li><?php echo $value ?></li> 
       <?php endforeach; ?> 
+1

是否連接工作?你是否得到了與數據庫的連接?什麼是錯誤?請提供更多信息 –

+0

Ups ..在li元素中忘記'回聲' –

+1

什麼是行不通的? –

回答

0

你嘗試用LIKE語句而不是=

請下面的代碼嘗試:

<?php 
    $db = mysqli_connect("server", "usr", "pw", "db"); 

    $FederalRes = $db->query("SELECT * FROM `sales` WHERE `state` LIKE 'Österreich'"); //gets every row with the state = 'Österreich' 

    $federals = array(); // empty array to collect federals 

    while ($row = $FederalRes->fetch_object()) { 
    if (!in_array($row->federal, $federals)) { // check if federal is already in array 
     array_push($federals, $row->federal); // adds federal to array 
    } 
    } 

    sort($federals); // sorts the array alphabetically 

    foreach ($federals as $key => $value): ?> 
    <li><?php echo $value ?></li> 
    <?php endforeach; ?> 
+0

哦非常感謝你!我不知道爲什麼,但它工作:) –

0
<?php 
try{ 
$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root',  ''); 
} 

catch(Exception $e){ 
die('Erreur : '.$e->getMessage()); 
} 

$answer= $conn->query("SELECT * FROM sales WHERE state LIKE 'Österreich' ORDER BY state ASC"); //sort it alphabetically here ;) 


while ($data = $answer->fetch()){ 
    echo $data['federal'] . '<br />'; 
} 
$reponse->closeCursor(); 

?>