我正在尝试使用PHP做待办事项

但出现此错误警告:非法字符串偏移量“ todo”

$userhastodo = "";

$toDo_Query = "SELECT * FROM `todo` WHERE `for`='".$_SESSION['id']."'";

$toDo_run_query =  mysqli_query($link , $toDo_Query);

$toDo_num_rows = mysqli_num_rows($toDo_run_query);


if($toDo_num_rows >= 1){
    $toDo_get_info = mysqli_fetch_array($toDo_run_query);

    $toDo_info = $toDo_get_info;

    foreach($toDo_info as $info){
        echo $info['todo'];

    }
}
else{
    $userhastodo = "You have no lists here :( !";
}


那是在我的todo.php页面的顶部

在html部分

        <?php if(empty($toDo_get_info)):?>
            <div class="empty">
                <i class="fa fa-meh-o fa-5x"></i>
                <h3>Strange, no lists found!</h3>
            </div>

            <?php else: ?>
                <ul class="list">
                <?php foreach($toDo_info as $info_in_toDo):?>
                    <li><span><?php echo $info_in_toDo['todo']?></span></li>
                <?php endforeach;?>
                </ul>
        <?php endif;?>


我怎样才能解决这个问题 ?它不断警告我=>警告:非法字符串偏移量'todo'

待办事项存在于我的mysql数据库中,其中包含待办事项活动的数据

最佳答案

如果您查看此部分:

$toDo_get_info = mysqli_fetch_array($toDo_run_query);

$toDo_info = $toDo_get_info;

foreach($toDo_info as $info){
    echo $info['todo'];
}


您会看到从结果集中获得一行,因此$toDo_info是包含一整行的一维数组。

您用foreach遍历该行,以便从该行获取所有值,它们是字符串,而不是数组。

您只需要:

echo $toDo_get_info['todo'];


编辑:要获取所有行并回显所有项目,您需要获取所有行:

while ($toDo_get_info = mysqli_fetch_array($toDo_run_query))
{
    // add your html to add list items here as well
    echo $toDo_get_info['todo'];
}

关于php - 警告:字符串偏移量'todo'|的PHP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29592007/

10-14 00:05