嗨,大家好,我的第一天用PHP。我想这对你来说很简单。有人可以帮我在$ result_device []中显示我的数组$ result []的每个元素作为字符串吗?
<?php
$mysqli = new mysqli("localhost", "root", "password", "db");
$sql = "SELECT erker, brunnen, stehlampe, computer, mediacenter FROM
devices";
if (!$result = $mysqli->query($sql)) {
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
$result = $result->fetch_row();
$i = 0;
foreach ($result as list ($result_device)) {
if ($result_device[i]==0){
$result_device[i] = "ausgeschaltet";
}
elseif ($result_device[i]==1){
$result_device[i] = "eingeschaltet";
}
$i++;
}
echo $result_device[0];
echo $result_device[1];
echo $result_device[2];
echo $result_device[3];
echo $result_device[4];
$result->close();
$mysqli->close();
?>
非常感谢你!
答案 0 :(得分:2)
您需要改进以下代码: -
<?php
$mysqli = new mysqli("localhost", "root", "password", "db");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result_device = [];
if ($result = $mysqli->query("SELECT erker, brunnen, stehlampe, computer, mediacenter FROM devices")) {
while ($row = $result->fetch_row()) {
print_r($row);
// Based on oputput modify condition and assign values to $result_device
}
$result->close();
}
$mysqli->close();
?>