如何使用php使用mysql数据库中的列号获取值

我想使用数字获取列的值是否可以在 php 中通过列号获取 $row 值


我想为列运行 foeach 循环并按列号获取值


try {

    $database =  new DB();

    $db = $database->Connect();


    foreach ($db->query($sql) as $row) {

        $data .='<tr>';

        $columnNumber = 0;


        foreach ($db->query($sql) as $column)

        {

            $data .= "<td>";

            $data .= $row[$columnNumber];

            $data .= "</td>";

        }

        $data .='</tr>';


    }

    $database->Close();

}

catch (PDOException $e)

{

    $data .= "There is some problem in connection: " . $e->getMessage();

}


MMTTMM
浏览 117回答 1
1回答

BIG阳

假设你只是想让你的代码工作,你可以这样做:foreach ($db->query($sql) as $row) {&nbsp; &nbsp; $data .='<tr>';&nbsp; &nbsp; foreach ($row as $column)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data .= "<td>";&nbsp; &nbsp; &nbsp; &nbsp; $data .= $column;&nbsp; &nbsp; &nbsp; &nbsp; $data .= "</td>";&nbsp; &nbsp; }&nbsp; &nbsp; $data .='</tr>';}或者,如果您真的需要知道列的“编号”,您可以这样做:foreach ($db->query($sql) as $row) {&nbsp; &nbsp; $data .='<tr>';&nbsp; &nbsp; foreach ($row as $columnNumber => $column)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data .= "<td>";&nbsp; &nbsp; &nbsp; &nbsp; $data .= $row[$columnNumber];&nbsp; &nbsp; &nbsp; &nbsp; $data .= "</td>";&nbsp; &nbsp; }&nbsp; &nbsp; $data .='</tr>';}
打开App,查看更多内容
随时随地看视频慕课网APP