0

I have implemented this and it returns an array as expected WITHIN the function, like so:

public function get_events()
    {


        $query = "SELECT * FROM events;";
        if($stmt = mysqli_prepare($this->Database, $query))
        {
            mysqli_stmt_bind_param($stmt, "s", $param);
            mysqli_stmt_execute($stmt);
            $resultObject = mysqli_stmt_get_result($stmt);
            mysqli_stmt_close($stmt);
        }

        $myArray = array();
        $size = mysqli_num_rows($resultObject);
        for($i = 0; $i < $size; $i++)
        {
            $myArray[$i] = mysqli_fetch_array($resultObject, MYSQLI_ASSOC);
        }

        echo '<pre>';
        print_r($myArray);

   }

/* CALL */
$Events = new Events();
$Events->get_events(); 

An array displays as i would expect.

However, when i set it to 'return $myArray' rather than 'print_r' and call the function again it returns:

/* CALL */
$Events = new Events();
$Events->get_events(); 
echo '<pre>';
print_r($Events);

This is returned -->

   Events Object
    (
    [Database] => mysqli Object
        (
            [affected_rows] => -1
            [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: f373ea5dd5538761406a8022a4b8a374418b240e $
            [client_version] => 50011
            [connect_errno] => 0
            [connect_error] => 
            [errno] => 0
            [error] => 
            [error_list] => Array
                (
                )

            [field_count] => 10
            [host_info] => localhost via TCP/IP
            [info] => 
            [insert_id] => 0
            [server_info] => 5.6.21
            [server_version] => 50621
            [stat] => Uptime: 100448  Threads: 1  Questions: 1057  Slow queries: 0  Opens: 124  Flush tables: 1  Open tables: 83  Queries per second avg: 0.010
            [sqlstate] => 00000
            [protocol_version] => 10
            [thread_id] => 186
            [warning_count] => 0
        )

I would like to be able to return an array so that I can use the query in multiple instances. Rather than echoing the html out within the function and having to repeat the same query elsewhere for the same data.

1
  • 1
    you are printing the object $event, if you want you can print_r($Events->get_events()) or just put it in a variable. Commented Jan 26, 2016 at 13:57

1 Answer 1

5
/* CALL */
$Events = new Events();
$myArray = $Events->get_events(); 
echo '<pre>';
print_r($myArray);

You have to assign your output to a variable.

Sign up to request clarification or add additional context in comments.

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.