Mysqli查询回声结果
Hi I am trying to make status of user so it will check how many post this user has and echo out the result, it was working fine when I was using mysql but after converting it to mysqli it giving me some errors
Here is my code:
<?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 }?>
Error:
Notice: Undefined offset: 1 in C:\xampp\htdocs\mysql_login\status.php on line 30
您好我正在尝试创建用户的状态,以便检查此用户的帖子数量并回显结果 ,当我使用mysql时工作正常,但在将其转换为mysqli之后它给了我一些错误 p>
这是我的代码: strong> p>
错误: strong> p>
\ n
&lt;?php
if(!isset($ _ COOKIE ['loggedin'])){
header(“location:index.php”);
}
session_start( );
if(!isset($ _ SESSION ['user'])){
header(“location:index.php”);
}
else {
?&gt;
\ n&lt;?php
$ con = mysqli_connect(“localhost”,“root”,“123”,“user”);
//检查连接
if(mysqli_connect_errno())
{
echo“失败 连接到MySQL:“。 mysqli_connect_error();
}
$ resule =“来自save_data的SELECT count(ID),其中username ='”。$ _ SESSION ['user']。“'”
或者死(mysql_error());
$ result = $ con&gt; query($ resule);
$ row = $ result-&gt; fetch_array(MYSQLI_NUM);
echo $ row [0],$ row [1]; \ n
?&gt;
&lt;?php}?&gt;
code> pre>
注意:未定义的偏移量:第30行的C:\ xampp \ htdocs \ mysql_login \ status.php中的1
code> pre>
div>
Try removing $row[1]
from:
echo $row[0], $row[1];
As the error says the offset 1 is undefined. so its pretty simple that you should remove $row[1]
from echo $row[0], $row[1];
The problem is $row[1]
in echo $row[0], $row[1];
because the $result->fetch_array
will only fill the $row[0]
, because the SQL Query
"SELECT count(ID) from save_data where username = '".$_SESSION['user']."'"
will only return one result if username is unique.
What will be happened If your query doesn't return any values? So better you check
if($resesult)
{
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];// remove $row[1] because it fills only the $row[0]
}