2014-10-30 36 views
1

我很新,所以我找不出要使用的代碼。我想從SQL問題中獲得一個對象,這樣我就可以使用$ thePerson-> FirstName等獲取數據。PHP:從一個函數獲取mysql數據作爲對象

我打電話的功能,因爲這:

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

我真的可以使用一些幫助改變代碼,所以它的工作原理。

+0

那麼最新的問題?你有沒有試過'echo $ thePerson-> FirstName'? – Ghost 2014-10-30 08:54:26

+0

「警告:mysqli_fetch_object()期望參數1是mysqli_result,布爾給定」是從數據庫選擇部分的錯誤,然後我得到「注意:嘗試獲取非對象的屬性」如果我嘗試「echo $ thePerson- > FirstName「 – 2014-10-30 10:01:11

回答

1

Afaik mysql_fetch_object不適用於預處理語句。見Is it possible to use mysqli_fetch_object with a prepared statement

嘗試在您的對帳單上使用->fetch(但之前使用->bind-result)。
Examples on php.net

或者使用->query,而不是你的準備/執行線,這應該返回設定->fetch_object使用的結果。
Examples on php.net

+0

謝謝,你的鏈接幫助我解決了這個問題,我只需要替換」$ theData = mysqli_fetch_object($ result);「 「$ theData = $ stmt-> get_result() - > fetch_object();」 – 2014-10-30 10:25:41

0

你需要使用ORM:對象關係映射器,如:http://www.doctrine-project.org/

這是一個有點先進的理念,你確定你需要在你的項目中使用呢?

+0

首選是對象,但我允許提供也是在二維數組 – 2014-10-30 09:13:13