php - 错误: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;
}
推荐阅读
- java - HttpServletRequest 从 HTML 表单中丢失参数
- python - 用 Python 中另一个 JSON 文件中的相应值替换 JSON 值
- sql-server - VB.NET、SQL Server 和日期列表
- node.js - 节点、纱线、Javascript - 安装 setprotocol 依赖项时出错
- javascript - AngularJS ng-model 无法绑定输入值
- python - 试图推送到 Heroku——为什么我会得到这个?错误:找不到满足要求的版本 python-3.8.6
- node.js - 首次关闭后无法重用 node-mongodb-native 客户端
- python - 如何在Python中删除连续重复的单词
- c# - 有没有办法在 C# 中使用 emguCV 比较两张脸?
- javascript - 为什么这个函数获取最大公约数不比较哪个数字更大