嗨,我正在尝试确定用户的状态,这样它会检查这个用户有多少帖子,并返回结果,当我使用mysql时,它运行良好,但在将其转换为mysqli后,它给了我一些错误
这是我的代码:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resule = "SELECT count(ID) from save_data where username = '".$_SESSION['user']."'"
or die(mysql_error());
$result = $con->query($resule);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0], $row[1];
?>
<?php }?>
错误:
Notice: Undefined offset: 1 in C:'xampp'htdocs'mysql_login'status.php on line 30
尝试从中删除$row[1]
echo $row[0], $row[1];
由于错误显示偏移量1未定义。因此,从echo $row[0], $row[1];
中删除$row[1]
非常简单
问题是echo $row[0], $row[1];
中的$row[1]
,因为$result->fetch_array
只会填充$row[0]
,因为SQL查询
"从save_data中选择count(ID),其中username='".$_SESSION['user']."'"
如果用户名是唯一的,将只返回一个结果。
如果您的查询没有返回任何值,会发生什么?所以你最好检查
if($resesult)
{
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];// remove $row[1] because it fills only the $row[0]
}