简体   繁体   中英

Error : Array to string conversion in PHP

I'm getting this error while trying to execute my function.

The columns of my database are 100% correct and i tested every request and it worked!

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

My function:

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

When using mysqli you need to free up the results per each query. Since you made a query with the $sql and then a second query using the $sqli without freeing up the results in between the two queries it caused you the problem.

Please read on these mysqli functions: http://php.net/manual/en/mysqli-result.free.php

Notice I closed your first connection and then made a second one. I think you can do this by keeping the same connection but calling mysqli_free_result() prior to your next query. I am not all that familiar with mysqli but I think that's right.

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM