0

The code below works fine. However I would like to output the results using a loop. I can do it by going through each key individually or as $post[0] for example but not using a loop to go through all the returned fields. All I get is one value of "Array". It looks like the entire array is inserted into a variable I'm not sure what's going on. I have tried http://www.hackingwithphp.com/5/3/0/the-two-ways-of-iterating-through-arrays. Any suggestions appreciated and also if anyone could explain what is going on that would be great. Thanks.

    $ID = $_POST['ID'];

    function query($ID){
        $servername = "x.x.x.x";
        $username = "xxxxx";
        $password = "xxxxx";
        $dbname = "xxxxx";

        $conn = mysqli_connect($servername, $username, $password, $dbname);
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        $query = "SELECT ID, NAME, POSITION, TELEPHONE_NUMBER, EMAIL FROM GROUP WHERE ID = '$ID'";
        $result = mysqli_query($conn, $sql);

        while($row = mysqli_fetch_array($result)){
            $resArr[] = $row;
        }
        return $resArr;
    }

    $person = query($ID);
    foreach($person as $post) {
    echo $post['ID'] . "<br>";
    echo $post['NAME'] . "<br>";
    echo $post['POSITION'] . "<br>";
    echo $post['TELEPHONE_NUMBER'] . "<br>";
    echo $post['EMAIL'] . "<br>";
}
?>
3
  • 5
    group is a reserved word. This cannot be your query. Busted. Commented May 6, 2015 at 9:16
  • Check for errors. Try ini_set("display_errors", 1); error_reporting(E_ALL); Show your entire code looks like you have an extra } closing braces after return $resArr; Commented May 6, 2015 at 9:23
  • Uchiha is almost right - the only error checking you've done here is on the mysqli_connect(). Enabling error reporting is good practice for a development machine but you should be explicitly polling for errors more often (particularly on mysqli_query() where you would have seen the problem reported by Strawberry) Commented May 6, 2015 at 9:30

2 Answers 2

2

Its not totally clear what you are asking but I assume you want to loop through the individual fields of the selected rows. As you built an array containing the query results each result itself being an array mysqli_fetch_array($result) then you can just add an inner loop to process the individual row array like so :-

$persons = query($ID);
foreach($persons as $person) {
    foreach ( $person as $fieldname => $value ) {
        echo $fieldname . '-' . $value . "<br>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Group is reserved word of mysql you can use it in backtics ``

$query = "SELECT ID, NAME, POSITION, TELEPHONE_NUMBER, EMAIL FROM `GROUP` WHERE ID = '$ID'";
    $result = mysqli_query($conn, $sql);

4 Comments

Its not braces its backtics Mate
I changed all variables...anyway this works fine I can use it all I want to know is if there is a better way that this can be done like start from field 0 and keep going until field x?
@John first of all change your MySQL query
thats what i mean thats not the proper sql query the query works fine everything works

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.