2

So I have found that when using db2 sql is inside a php function it does not return any values.

It fails on DB2_PREPARE($conn, $sql2);

Where as if you execute this code outside of the function it does its job perfectly fine and returns all of the results.

Code:

        function getRooms(){
            $sql2 = ("SELECT * FROM WS_ASSETS_rooms");
                    $stmt2 = db2_prepare($conn, $sql2);
                     if ($stmt2) {
                            $result2 = db2_execute($stmt2);
                            if (!$result2) {
                                     return "exec errormsg: " .db2_stmt_errormsg($stmt2);
                            }

                            while ($row = db2_fetch_array($stmt2)) {
                                    echo "$row[1]";
                            }
                    } else {
                            return "exec errormsg: " .db2_stmt_errormsg($stmt2);
                    }
};
    echo getRooms();

This returns "exec errormsg:" and no error.

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33
Austin
  • 61
  • 1
  • 6

1 Answers1

2

$conn is not in scope. You have to either pass the variable to the function (recommended) or use the global keyword.

Passing the variable

function getRooms($conn){
    // ...
}

echo getRooms($conn);

Using global

function getRooms(){
    global $conn;
    // ...
}

echo getRooms();
chrislondon
  • 12,487
  • 5
  • 26
  • 65