我这里有一个函数,在登录后调用数据库中的详细信息。我遇到的问题是我收到了这个:
警告:mysqli_fetch_array()期望参数1为mysqli_result, 布尔值在...中给出。
请您指出需要更改的内容以及如何从数据库中获取所需的数据?
function profile(){
include "config.php";
$conn = mysqli_connect($host,$user,$pass,$db);
//check
if( mysqli_connect_errno($conn) ){
echo "Error in DB";
}else{
//connected
}
//prepare
$sql = "SELECT * FROM accounts where username='" . $_SESSION['username'] . "'";
//display
$result = mysqli_query($conn,$sql);
$myrow=mysqli_fetch_array($result) ){ // --->the warning is from this line
do{
$info= array();
$info['firstName'] = $myrow['firstName'];
$info['middleInitial'] = $myrow['middleInitial'];
$info['lastName'] = $myrow['lastName'];
$info['mobile_no'] = $myrow['mobile_no'];
$info['email_address'] = $myrow['email_address'];
$info['registration_date'] = $myrow['registration_date'];
$account[] = $info;
}while($myrow=mysqli_fetch_array($result));
}
return $account;
}
答案 0 :(得分:1)
$result
为null
。
来自PHP文档mysqli_query
:
失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或 EXPLAIN查询mysqli_query()将返回一个mysqli_result对象。对于 其他成功的查询mysqli_query()将返回TRUE。
答案 1 :(得分:0)
USE
if($result){
$myrow=mysqli_fetch_array($result) ){
do{
$info= array();
$info['firstName'] = $myrow['firstName'];
$info['middleInitial'] = $myrow['middleInitial'];
$info['lastName'] = $myrow['lastName'];
$info['mobile_no'] = $myrow['mobile_no'];
$info['email_address'] = $myrow['email_address'];
$info['registration_date'] = $myrow['registration_date'];
$account[] = $info;
}while($myrow=mysqli_fetch_array($result));
}
return $account;
}
答案 2 :(得分:0)
mysqli_affected_rows()函数返回上一个SELECT,INSERT,UPDATE,REPLACE或DELETE查询中受影响的行数。
<?php
//display
$result = mysqli_query($conn,$sql);
$count=mysqli_affected_rows($con);
if($count>0)
{
$myrow=mysqli_fetch_array($result) ){
do{
$info= array();
$info['firstName'] = $myrow['firstName'];
$info['middleInitial'] = $myrow['middleInitial'];
$info['lastName'] = $myrow['lastName'];
$info['mobile_no'] = $myrow['mobile_no'];
$info['email_address'] = $myrow['email_address'];
$info['registration_date'] = $myrow['registration_date'];
$account[] = $info;
}while($myrow=mysqli_fetch_array($result));
}
return $account;
}
else
{
echo "empty";
}
?>