PHP注意:未定义偏移量1 [英] PHP Notice: Undefined offset 1

查看:152
本文介绍了PHP注意:未定义偏移量1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中顺序获取id列.我已经编写了这段代码,但是没有用.

I am trying to fetch an id column from database sequentially. I have written this code but it didn't work.

 $connection = mysqli_connect($servername,$username,$password,$dbname);
if(!$connection)
    die("Database connection failed: " . mysqli_connect_error());

$query = "SELECT ID FROM channel "; 

if($result = mysqli_query($connection,$query))
{
    $count = mysqli_num_rows($result);// it will start from the first row and continue with others...
    //fetch one and one row
    while($row=mysqli_fetch_row($result))
    {
        for($i=0; $i<$count; $i++)
        echo "<a>$row[$i]</a>";
    }
    //free result set 
    mysqli_free_result($result);

}
mysqli_close($connection);

所以,这段代码给了我这个错误:

So, this code gives me this error:

注意:未定义的偏移量:第24行的C:\ xampp \ htdocs \ gt \ fetch_channel.php中的1

Notice: Undefined offset: 1 in C:\xampp\htdocs\gt\fetch_channel.php on line 24

有人可以帮我吗?

推荐答案

问题是:$ row中没有$ i索引 我不确定您想要什么,但是我想您想要这样的东西:

the problem is: There is no $i index in $row I'm not sure what would you like, but I think you would like something like this:

 $connection = mysqli_connect($servername,$username,$password,$dbname);
if(!$connection)
    die("Database connection failed: " . mysqli_connect_error());

$query = "SELECT ID FROM channel "; 

if($result = mysqli_query($connection,$query))
{

    //fetch one and one row
    while($row=mysqli_fetch_row($result))
    {

        echo "<span>".$row['id']."</span><br/>";
    }


}
mysqli_close($connection);

这篇关于PHP注意:未定义偏移量1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆