Patrik Blå
Patrik Blå

Reputation: 79

PHP: Get mysql data from a function as an object

I'm pretty new to this so I can't figure out the code to use. I want to get an object from a SQL question so I can get data with $thePerson->FirstName and so on.

I'm calling the function as this:

$thePerson = getPerson(1);

and this is the function so far:

function getPerson ($person) {
    global $con;
    $stmt = $con->prepare("SELECT * FROM personal WHERE IDA=?");
    $stmt -> bind_param('i', $person);
    $result = $stmt -> execute();
    $theData = mysqli_fetch_object($result);
    $stmt -> close();
    return $theData;
}

I could really use some help changing the code so it works.

Upvotes: 1

Views: 70

Answers (2)

wolfgangwalther
wolfgangwalther

Reputation: 1236

Afaik mysql_fetch_object doesn't work with prepared statements. See Is it possible to use mysqli_fetch_object with a prepared statement.

Try to either use ->fetch on your statement (but use ->bind-result before).
Examples on php.net

Or use ->query instead of your prepare / execute lines, which should return a result set to use with ->fetch_object.
Examples on php.net

Upvotes: 1

osmancode
osmancode

Reputation: 291

you need to use ORM: Object Relational Mapper like: http://www.doctrine-project.org/

this is a bit advanced concept, are you sure you need to use this in your project?

Upvotes: 0

Related Questions