0

As stated in the title, I'm trying to load a single row of user information into an array, preferrably associative. Here's what I have so far:

function load_User_Info($id) {
        $parameters = array();
        $results = array();

        $query = "SELECT * FROM users WHERE User_ID=$id";
        $stmt = $this->conn->prepare($query) or die('Error preparing query');
            $stmt->execute();
            $stmt->store_result();
            $meta = $stmt->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }

            call_user_func_array(array($stmt, 'bind_result'), $parameters);

            while ($stmt->fetch()) {
                $x=array();
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
            }
            $results[] = $x;
    return $results;
    }

This is found inside a Mysql class, and here's where it's being used on the page:

Hi, <?php
            $mysql = New Mysql();
            session_start();
            $result = array();
            $result = $mysql->load_User_Info($_SESSION['userid']);
            echo $result['firstname'] . " " . $result['lastname'];
    ?>!

Oh the page however, it only displays "Hi, !." I've looked at the mysqli_stmt_fetch page in the PHP manual, and a couple examples were provided from other users and neither of those worked for me either. Thanks in advance.

2
Contributors
4
Replies
9
Views
4 Years
Discussion Span
Last Post by randomkid73
3

@randomkid73

As stated in the title, I'm trying to load a single row of user information into an array, preferrably associative. Here's what I have so far:

It's weird you have a $stmt->fetch() to fetch the array but I don't see you having $stmt->fetch_assoc() to fetch associative array.

Read here for the fetch_assoc()

http://php.net/manual/en/mysqli-result.fetch-assoc.php

The issue is here:

call_user_func_array(array($stmt, 'bind_result'), $parameters);

while ($stmt->fetch()) {

$x=array();

foreach ($row as $key => $val) {

$x[$key] = $val;

}

}

I think you need something like this to separate the arrays:

while ($stmt->fetch_assoc($result)) {
        echo $result['firstname'];
        echo $result['lastname'];
    } 

Edited by LastMitch: grammer

Votes + Comments
To Rectify what some retard did to LastMitch
0

I came across fetch_assoc() too but everything I came across when researching (http://us2.php.net/manual/en/mysqli-stmt.fetch.php, for example) pointed to the way I had it done above, something about the prepared statements? I think it had to do with selecting the entire row, and binding variables to each field was too rigid/tedious.

In any case, I still don't have it working.

0

@randomkid73

Are you connected to the db and the query is running?

$stmt = $this->conn->prepare($query) or die('Error preparing query');

Why did you put conn where I don't see it anywhere in your code?

Take it out and try it now:

$stmt = $this->prepare($query) or die('Error preparing query');

Edited by LastMitch: grammer

1

I didn't show it, but $conn was defined as a private var for the class, and then used as the object of the connection.

I've fixed it now though. The problem was that I was returning the array as a result of the load_User_Info() function, but that doesn't keep it as an associative array. When I tried to echo the result using the key, it didn't exist. I've fixed it by instead passing the result array as reference to the function, then using it as below:

//located in Mysql class
function load_User_Info($id, &$result = array()) {
        $query = "SELECT FirstName, LastName FROM users WHERE User_ID=$id";
            $stmt = $this->conn->query($query);
            $result= $stmt->fetch_array(MYSQLI_ASSOC);
            //echo $row['FirstName'] . ' ' . $row['LastName'];
            $stmt->free();
            $stmt->close(); 
            return true;
    }

//usage inline
    $mysql = New Mysql();
    $data = array();
    $mysql->load_User_Info($_SESSION['userid'], $data) or die ('Error loading data');
    echo $data['FirstName'] . ' ' . $data['LastName'];

Thanks for your help though!

Edited by randomkid73: format

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.