mysql&;php select*查询不工作
我在数据库中有一个名为sales的表。每行在federal列中都有一个值。 所以我想在这段代码中收集一个数组中的所有联邦成员,但它不起作用,我不知道为什么。。。请帮忙!谢谢mysql&;php select*查询不工作,php,html,mysql,Php,Html,Mysql,我在数据库中有一个名为sales的表。每行在federal列中都有一个值。 所以我想在这段代码中收集一个数组中的所有联邦成员,但它不起作用,我不知道为什么。。。请帮忙!谢谢 <?php $db = mysqli_connect("server", "usr", "pw", "db"); $FederalRes = $db->query("SELECT * FROM `sales` WHERE `state` = 'Öste
<?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; ?>
您尝试使用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; ?>
连接工作正常吗?你和db有联系吗?怎么了?请提供更多信息。。在li元素中忘记了“echo”什么不起作用?您可能希望在foreach循环中使用$federals
而不是$variable
。连接工作正常,没有错误。。仅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();
?>