PHP函数循环SQL结果并生成HTML表

PHP函数循环SQL结果并生成HTML表,php,html,mysql,Php,Html,Mysql,我想编写一个php函数,它将以特定的方式回显sql查询结果 例如,表1为10.000行*43列: 没有名字和出生日期 1约翰教师1987年 2 1976年工程师 3. 4. 五, 等等。基于1-10.000的整数No,我想生成如下表: 姓名:约翰 教授:老师 出生日期:1987年 姓名:尼克 教授:工程师 出生日期:1976年 到目前为止,我的代码: ` $hostname = ""; $username = ""; $password = ""; $d

我想编写一个php函数,它将以特定的方式回显sql查询结果

例如,表1为10.000行*43列:

没有名字和出生日期 1约翰教师1987年 2 1976年工程师 3. 4. 五,

等等。基于1-10.000的整数No,我想生成如下表:

姓名:约翰 教授:老师 出生日期:1987年

姓名:尼克 教授:工程师 出生日期:1976年

到目前为止,我的代码:

`
    $hostname = "";     
    $username = "";
    $password = "";
    $dbname = "db1";

    //connection to the database
    $con = mysqli_connect($hostname, $username, $password, $dbname)
        or die("Unable to connect to MySQL");
    mysqli_set_charset($con, 'utf8');
    /*echo "Connected to db1 database <br>";
    else
    echo "Could not connect"; */

    $query1 = "SELECT * FROM `table 1` WHERE CODE_NO = 1";
    $query2 = "SHOW COLUMNS FROM table 1";
    $result1 = mysqli_query($con, $query1);
    $result2 = mysqli_query($con, $query2);

    // Check result
    // Useful for debugging.
     if (!$result1) {
        $message  = 'Invalid query: ' . mysqli_error($con) . "\n";
        $message .= 'Whole query: ' . $query1;
        die($message);
    }

    echo "<table>"; // table tag in the HTML
        while($row = mysqli_fetch_array($result1))
        //Creates a loop to loop through results
        {
        echo

        "<tr>
            <th>CODE_NO:</th>
            <td>" . $row['CODE_NO'] . "</td>
        </tr>
        <tr>
            <th>FIRST_NAME:</th>
            <td>" . $row['FIRST_NAME'] . "</td>
        </tr>
        <tr>
            <th>SURNAME:</th>
            <td>" . $row['SURNAME'] . "</td>
        </tr>
        <tr>
            <th>Date of birth:</th>
            <td>" . $row['DOB'] . "</td>
        </tr>
        <tr>
            <th>Date of death:</th>
            <td> " . $row['DOD'] . "</td>
        </tr>";
        }
    echo "</table>"; //Close the table in HTML

    ?>`
是否有可能只对流程进行一次编码,而不进行硬编码,这样就可以根据需要重复多次

编辑:

$x=1; $query=SELECT*FROMpersonsWHERE CODE_NO='$x';
$result=mysqli\u query$con$query

您可以得到例外结果。 您需要有一个变量,该变量随值递增,并在末尾返回

参见我的示例:

// Check result
// Useful for debugging.
 if (!$result1) {
    $message  = 'Invalid query: ' . mysqli_error($con) . "\n";
    $message .= 'Whole query: ' . $query1;
    die($message);
}

$table = "<table>"; // table tag in the HTML
    while($row = mysqli_fetch_array($result1)){
    //Creates a loop to loop through results
    $table .="<tr>";
    $table .="  <th>CODE_NO:</th>";
    $table .="  <td>" . $row['CODE_NO'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="    <th>FIRST_NAME:</th>";
    $table .="    <td>" . $row['FIRST_NAME'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="    <th>SURNAME:</th>";
    $table .="    <td>" . $row['SURNAME'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="   <th>Date of birth:</th>";
    $table .="   <td>" . $row['DOB'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="    <th>Date of death:</th>";
    $table .="    <td> " . $row['DOD'] . "</td>";
    $table .="</tr>";
    }
$table .= "</table>"; //Close the table in HTML
echo $table;
?>`
这是获得您想要的结果的一种可能的方法。
但是,您发送了一个json格式的数组,其中包含值,并在前端使用js构建表。

如果我理解正确,这将满足您的需要:

function generateTableFromResult($result) {
   $html = "<table>";
   while($row = mysqli_fetch_array($result)) {
      foreach($row as $column => $value) {
        $html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
      }
   }
   $html.="</table>";
   return $html;
}

// usage:
// ...
$result1 = mysqli_query($con, $query1);
echo generateTableFromResult($result1);

修改了功能generateTableFromResult,从上面正确运行

function generateTableFromResult($result) {
   $html = "";
   $i = 0;
   $header = "<tr>";
   $body = "";
   while($row = mysqli_fetch_assoc($result)) {
      $i += 1;
      
      $body .= "<tr>\n";
      foreach($row as $column => $value) {
          if ($i == 1){
              $header .= "<th>$column</th>\n";
          }
         $body .= "<td>$value</td>\n";
      }
      $body .= "</tr>\n";
   }
   $header .= "</tr>\n";
   $html .= "<table> $header $body</table>";
   return $html;
}

是否有可能只对流程进行一次编码,而不进行硬编码,这样就可以根据需要重复多次。。。你这到底是什么意思?是的。照你说的做。将行放在一个数组中,循环行数组和列数组。@CD001最后几行可能不需要,因为它们看起来很混乱。如果你愿意的话,你可以把它们编辑掉,因为上面已经清楚地描述了这个问题。我得到了语法错误,意外的'while'T\u while。如果我删除它,尽管您的代码似乎工作正常。0 1代码1 1 1ΑΑΑΗΗΗΗΗΗΗΗΗΗΗΗΗΗ∑∑∑∏∏∏∧∧∑是您的循环返回的值。如果您不想像示例中那样为每一列创建一行,请将其设置为verticallywell!将和放在内部循环之外。我不熟悉js,但感谢您的回答。我也尝试了您的代码,尽管我没有收到错误,但html中没有显示任何内容。html中没有显示任何内容-因为这个$table=;最后。应为$table.=;对我有一个失误,应该是$table;代替$table=;您不提供增量位。我添加了一个变量$x=1;我应该把$x=$x+1放在哪里;为了工作而分开?