PHP:从函数中获取mysql数据作为对象

PHP:从函数中获取mysql数据作为对象,php,mysql,Php,Mysql,我对这个很陌生,所以我想不出要使用的代码。我想从一个SQL问题中获取一个对象,这样我就可以获取带有$thePerson->FirstName等的数据 我调用函数的方式如下: $thePerson = getPerson(1); 这是目前为止的功能: function getPerson ($person) { global $con; $stmt = $con->prepare("SELECT * FROM personal WHERE IDA=?"); $stm

我对这个很陌生,所以我想不出要使用的代码。我想从一个SQL问题中获取一个对象,这样我就可以获取带有$thePerson->FirstName等的数据

我调用函数的方式如下:

$thePerson = getPerson(1);
这是目前为止的功能:

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;
}

我真的需要一些帮助来更改代码以使其正常工作。

您需要使用ORM:Object Relational Mapper,如:


这是一个有点高级的概念,您确定需要在项目中使用它吗?

Afaik
mysql\u fetch\u object
不适用于准备好的语句。看

尝试在语句中使用
->fetch
(但在前面使用
->bind result

或者使用
->query
而不是prepare/execute行,prepare/execute行应返回与
->fetch\u对象一起使用的结果集


那么问题出在哪里?您是否尝试过
echo$thePerson->FirstName
yourself?“警告:mysqli_fetch_object()期望参数1为mysqli_result,boolean given”是数据库选择部分的错误,然后是ofc I get“注意:如果我尝试“echo$thePerson->FirstName”,则“尝试获取非对象的属性”首选的是对象,但我可以提供一个二维数组,您的链接帮助我解决了这个问题,我所要做的就是将“$theData=mysqli_fetch_object($result);”替换为“$theData=$stmt->get_result()->fetch_object()”