簡體   English   中英

錯誤:PHP中數組到字符串的轉換

[英]Error : Array to string conversion in PHP

嘗試執行我的功能時出現此錯誤。

我數據庫的列是100%正確的,我測試了每個請求,它奏效了!

Error :

[26-May-2018 05:21:49 UTC] PHP Notice:  Array to string conversion in C:\wamp64\www\gcm\database.php on line 82

[26-May-2018 05:21:49 UTC] PHP Warning:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\gcm\database.php on line 88

我的功能:

function getHistoriqueNotification($user,$mdp){
$com = new DbConnect();
$message=array();
$sql="select UTLR_UID from adm_utilisateurs where UTLR_LOGIN='$user' and UTLR_MDP='$mdp'";
$result=mysqli_query($com->getDb(),$sql);
$getID = mysqli_fetch_assoc($result);
$userID = $getID['UTLR_UID'];
$sqli = "SELECT alr_alertes.ALRT_DES_LN1,alr_alertes.ALRT_PHOTO,alr_historiques.AHIS_DES_LN1,alr_historiques.AHIS_DATEHEURE from alr_alertes,alr_historiques WHERE alr_alertes.ALRT_UID=alr_historiques.ALRT_UID AND alr_historiques.UTLR_UID=$userID";
$resulti = mysqli_query($com->getDb(),$sqli);
while($row=mysqli_fetch_assoc($resulti)){
$message = array('photo' =>$row['ALRT_PHOTO'] , 'titre' => $row['ALRT_DES_LN1'] , 'dateHeure' =>$row['AHIS_DATEHEURE'] , 'detail' => $row['AHIS_DES_LN1']);
}

return $message;
}

使用mysqli時,您需要釋放每個查詢的結果。 由於您使用$ sql進行了查詢,然后使用$ sqli進行了第二次查詢,而又沒有釋放兩次查詢之間的結果,因此導致了問題。

請閱讀以下mysqli函數: http : //php.net/manual/en/mysqli-result.free.php

注意,我關閉了您的第一個連接,然后建立了第二個連接。 我認為您可以通過保持相同的連接但在下一個查詢之前調用mysqli_free_result()來實現此目的。 我對mysqli並不很熟悉,但是我認為是對的。

function getHistoriqueNotification($user, $mdp){

$com = new DbConnect();
$message=array();
$sql = "select UTLR_UID from adm_utilisateurs where UTLR_LOGIN='$user' and UTLR_MDP='$mdp'";
$result = mysqli_query($com->getDb(),$sql);
$getID = mysqli_fetch_assoc($result);
$userID = $getID['UTLR_UID'];

mysqli_close($com);
unset($com);

$com1 = new DbConnect(); //This is really just a test.

$sqli = "SELECT alr_alertes.ALRT_DES_LN1,alr_alertes.ALRT_PHOTO,alr_historiques.AHIS_DES_LN1,alr_historiques.AHIS_DATEHEURE from alr_alertes,alr_historiques WHERE alr_alertes.ALRT_UID=alr_historiques.ALRT_UID AND alr_historiques.UTLR_UID=$userID";

if ($resulti = mysqli_query($com1->getDb(), $sqli)){

    while($row = mysqli_fetch_assoc($resulti)){

    //Added the [] after message.
    $message[] = array(
      'photo'     =>$row['ALRT_PHOTO'], 
      'titre'     =>$row['ALRT_DES_LN1'], 
      'dateHeure' =>$row['AHIS_DATEHEURE'],
      'detail'    =>$row['AHIS_DES_LN1']
    );

  }

}else{

  echo("Error description: " . mysqli_error($com1->getDb()));

  }

print_r($message); //<---This will show you if you have a result.
return $message;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM