在HTML表格中显示MySQL查询结果

时间:2016-05-03 14:03:37

标签: php html mysql html-table

我试图在HTML表格中显示MySQL的一些查询结果。我试图在一个新的PHP页面中显示该表,该工作正常。我的问题是,我似乎找不到在同一个html页面中显示表格的方法。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="relative.css" media="screen" />

</head>

<body>

<div class="cabecera">
    <h1> GENUS </h1>
</div>

<ul id="menu">
    <li><a href="profile.html">Perfil</a></li>
    <li><a href="member.html">Nuevo pariente</a></li>   
    <li><a href="relative.html" class="relative">Parientes registrados</a></li>
</ul>

<?php

 require_once 'connectbd.php';
 $conn = connectbd();


session_start();
$escritor = $_SESSION["escritor"];

$sql="SELECT * FROM Pariente WHERE escritor_ID='$escritor' ";

$result= mysql_query($conn,$sql) or die(mysql_error($conn));
if(mysql_num_rows($result)==0) die("No hay registros para mostrar");

echo '<table border=1 cellpadding=4 cellspacing=0>';

 echo '<tr>
     <th colspan=5> Agenda personal </th>
   <tr>
     <th> ID </th><th> Nombre </th><th> Primer Apellido </th>
  </tr>';

while($res=mysql_fetch_array($result))
{

echo'<tr><td>'.$res['nombre'].'</td><td>'.$res['primer_apellido'].'</td><td>'.$res['segundo_apellido'].'</td></tr>';

}
echo '</table>';


?>

<a href="logout.php"><input type="button" value="Cerrar sesión" /></a>

</body>

</html>

这是我的connectbd.php代码:

<?php

class DB_Connect {

// constructor
function __construct() {

}

// destructor
function __destruct() {
    // $this->close();
}

// Connecting to database
public function connect() {
    require_once 'config.php';
    // connecting to mysql
    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    // selecting database
    mysql_select_db(DB_DATABASE);

    // return database handler
    return $con;
}

// Closing database connection
public function close() {
    mysql_close();
}

}

?>

编辑:这是显示的表格:

enter image description here

0 个答案:

没有答案