0

In trying to pass an array function from one php page to another. page try.php has the actual function

function someFunc() {

include_once("includes/dbconnect.php");
$types = array();
$connect = TRUE;
$dbconn = dbconnect($connect);

if ($dbconn) {
    $sql = "select usertype, userdeptnum from bd.bdusers";

    $stmt = db2_prepare($dbconn, $sql);
    if ($stmt) {
        $result = db2_execute($stmt);
        if (!$result) {
            echo "exec errormsg: " . db2_stmt_errormsg($stmt);
        }

        while ($row = db2_fetch_assoc($stmt)) {
            $types[] = $row;

        }
    } else {
        echo "exec errormsg: " . db2_stmt_errormsg($stmt);
    }
    db2_close($dbconn);
} else {
    echo "faild " . db2_conn_errormsg();
}

return array($types);
}

If i use

 echo "<pre>";
 print_r($types);
 echo "</pre>";

i get this

Array
(
[0] => Array
    (
        [USERTYPE] => 1
        [USERDEPTNUM] => 3
    )

[1] => Array
    (
        [USERTYPE] => 1
        [USERDEPTNUM] => 5
    )

[2] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 28
    )

[3] => Array
    (
        [USERTYPE] => 1
        [USERDEPTNUM] => 28
    )

[4] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 3
    )

[5] => Array
    (
        [USERTYPE] => 1
        [USERDEPTNUM] => 1
    )

[6] => Array
    (
        [USERTYPE] => 1
        [USERDEPTNUM] => 1
    )

[7] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 3
    )

[8] => Array
    (
        [USERTYPE] => 1
        [USERDEPTNUM] => 31
    )

[9] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 2
    )

[10] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 1
    )

[11] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 56
    )

[12] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 89
    )

[13] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 56
    )

[14] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 45
    )

[15] => Array
    (
        [USERTYPE] => 2
        [USERDEPTNUM] => 90
    )

)

But when I pass this function on try2.php and so the same pre, print_r to see what I get I get this

Array
(
[0] => Array
    (
        [0] => Array
            (
                [USERTYPE] => 1
                [USERDEPTNUM] => 3
            )

        [1] => Array
            (
                [USERTYPE] => 1
                [USERDEPTNUM] => 5
            )

        [2] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 28
            )

        [3] => Array
            (
                [USERTYPE] => 1
                [USERDEPTNUM] => 28
            )

        [4] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 3
            )

        [5] => Array
            (
                [USERTYPE] => 1
                [USERDEPTNUM] => 1
            )

        [6] => Array
            (
                [USERTYPE] => 1
                [USERDEPTNUM] => 1
            )

        [7] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 3
            )

        [8] => Array
            (
                [USERTYPE] => 1
                [USERDEPTNUM] => 31
            )

        [9] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 2
            )

        [10] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 1
            )

        [11] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 56
            )

        [12] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 89
            )

        [13] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 56
            )

        [14] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 45
            )

        [15] => Array
            (
                [USERTYPE] => 2
                [USERDEPTNUM] => 90
            )

    )

)

It's an array inside an array. Ive tried to echo out the array using foreach, and with a for loop, but I alway get undefined index or Array to string conversion and its just prints out "Array". What am I doing wrong?

0

2 Answers 2

3

Because you returned array($types);. Return $types instead of array($types).

function someFunc() {
    include_once("includes/dbconnect.php");
    $types = array();
    $connect = TRUE;
    $dbconn = dbconnect($connect);
    if ($dbconn) {
        $sql = "select usertype, userdeptnum from bd.bdusers";
        $stmt = db2_prepare($dbconn, $sql);
        if ($stmt) {
            $result = db2_execute($stmt);
            if (!$result) {
                echo "exec errormsg: " . db2_stmt_errormsg($stmt);
            }
            while ($row = db2_fetch_assoc($stmt)) {
                $types[] = $row;

            }
        } else {
        echo "exec errormsg: " . db2_stmt_errormsg($stmt);
    }
    db2_close($dbconn);
    } else {
        echo "faild " . db2_conn_errormsg();
    }
    return $types; //<==
}

EDIT : In try.php try this code to show result in table

echo '<table border="1">';
echo '<tr><th>USERTYPE</th><th>USERDEPTNUM</th></tr>';
foreach($types as $val){
    echo '<tr><td>'.$val['USERTYPE'].'</td><td>'.$val['USERDEPTNUM'].'</td></tr>';
}
echo '</table>';
Sign up to request clarification or add additional context in comments.

1 Comment

I changed that one line and refreshed try2.php and with pre I get the correct looking array but the foreach gives me this error "Notice: Array to string conversion in /www/websvr6/try2.php on line 17 Array" What i'm trying to do is populate a table with this array. Is there a better way to do this?
0
<?php
include_once("try.php");


$types = someFunc();

echo "<pre>";
print_r(someFunc());
echo "</pre>";

foreach ($types as $list){
echo $types;

}
?>

Comments

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.