Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表中不显示通过PHP获取的第一行mysql数据_Php_Html - Fatal编程技术网

表中不显示通过PHP获取的第一行mysql数据

表中不显示通过PHP获取的第一行mysql数据,php,html,Php,Html,我已经编写了一个函数来显示使用PHP mysqli_query$db_conn$sql从MySQL数据库获取的数据;当我调用以下显示功能时,数据的第一行不显示。请建议我怎样才能到达第一排 function display_data($retval) { $output = '<table border="1">'; foreach($retval as $key => $var) { $output .= '<tr>'; foreach($var

我已经编写了一个函数来显示使用PHP mysqli_query$db_conn$sql从MySQL数据库获取的数据;当我调用以下显示功能时,数据的第一行不显示。请建议我怎样才能到达第一排

function display_data($retval) {
$output = '<table border="1">';
foreach($retval as $key => $var) {
    $output .= '<tr>';

    foreach($var as $k => $v) {
        if ($key === 0) {
            $output .= '<td><strong>' . $k . '</strong></td>';
        } else {
            $output .= '<td>' . $v . '</td>';
        }
    }
    $output .= '</tr>';
}
$output .= '</table>';
echo $output;
}
编辑2:

我能够通过以下代码实现这一点,请建议是否有更好的方法显示在HTML/网页中

//code to display data in table form
function display_data($retval)
{
    $output = '<table border="1">';
    foreach ($retval as $key => $var)
    {

        echo $key;

        if ($key === 0)
        {
            $output .= '<tr>';
            foreach ($var as $k => $v)
            {
                $output .= '<th><strong>' . $k . '</strong></th>';

            };
            $output .= '</tr>';
        };
        $output .= '<tr>';
        foreach ($var as $k => $v)
        {
            if ($key === 0)
            {
                $output .= '<td>' . $v . '</td>';
                echo $v;
            }
            else
            {
                $output .= '<td>' . $v . '</td>';
            };
        };

        $output .= '</tr>';
    }
    $output .= '</table>';
    echo $output;
}

无法看到第一行的原因是您从未显示它。如果迭代在第一行,则只显示标题,但跳过数据

foreach ($var as $k => $v) {
    if ($key === 0) {
        // If this is the first row, you display the key, but not the value.
        $output .= '<td><strong>' . $k . '</strong></td>';
    } else {
        $output .= '<td>' . $v . '</td>';
    }
}
function display_data1($retval) {
    $output = '<table border="1">';
    foreach ($retval as $key => $var) {
        if ($key === 0) {
            // If key is 0, meaning first row...
            $output .= '<thead><tr>';
            // create new HTML row and loop the first MySQL row 
            foreach ($var as $k => $v) {
                $output .= '<th>' . $k . '</th>';
            }
            $output .= '</tr></thead>';
        }

        // Then loop as normal all rows including the first one.
        $output .= '<tr>';
        foreach ($var as $k => $v) {
            $output .= '<td>' . $v . '</td>';
        }
        $output .= '</tr>';
    }

    $output .= '</table>';
    echo $output;
}
你建议的解决方案是正确的。您需要将第一行循环两次。一次显示标题,然后一次显示数据

foreach ($var as $k => $v) {
    if ($key === 0) {
        // If this is the first row, you display the key, but not the value.
        $output .= '<td><strong>' . $k . '</strong></td>';
    } else {
        $output .= '<td>' . $v . '</td>';
    }
}
function display_data1($retval) {
    $output = '<table border="1">';
    foreach ($retval as $key => $var) {
        if ($key === 0) {
            // If key is 0, meaning first row...
            $output .= '<thead><tr>';
            // create new HTML row and loop the first MySQL row 
            foreach ($var as $k => $v) {
                $output .= '<th>' . $k . '</th>';
            }
            $output .= '</tr></thead>';
        }

        // Then loop as normal all rows including the first one.
        $output .= '<tr>';
        foreach ($var as $k => $v) {
            $output .= '<td>' . $v . '</td>';
        }
        $output .= '</tr>';
    }

    $output .= '</table>';
    echo $output;
}

警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行

显示调用display_数据的代码代码已更新代码您尝试调试该问题的原因是什么?此外,请注意,您的代码对SQL是开放的injections@NicoHaase是的,我尝试过调试这个问题,因为我对HTML和PHP是新手。您能告诉我SQLi的代码是如何打开的吗?是的,您提出的解决方案是正确的。