0

hi guys its my first day with php. i guess this is an easy one for you. can someone help me to display each element of my array $result[] as a string in $result_device[]?

<?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();
  ?>

thank you very much!

7
  • First print your result in side the foreach. print_r($result_device) and check your array and add your array here Commented Feb 14, 2018 at 4:32
  • You used a foreach loop just a few lines above. Have a look at the manual php.net/manual/en/control-structures.foreach.php and i think you will sort it out. Commented Feb 14, 2018 at 4:32
  • This doesn't seem right $result = $result->fetch_row(); Do you really want to over-write the object? Commented Feb 14, 2018 at 4:36
  • i is not going to use the declared $i value. In fact, you don't need to use a counter ($i) if you are just generating an indexed array. You can push values into your result array, by using $result_device[]='whatever'; What coding tutorials did you read to arrive at this code? Commented Feb 14, 2018 at 4:38
  • its okay for me to overwrite $result, then i dont need another variable i guess or? Commented Feb 14, 2018 at 4:41

1 Answer 1

2

You need to improve your code like below:-

<?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();
?>
Sign up to request clarification or add additional context in comments.

3 Comments

ok thank you very much!. im trying to understand and i go on from there!
i spent some time with it but finally it works. thanks for your help
@KaiH. glad to help you and it's really good that you tried yourself.That's how you will learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.