我是PHP和Mysql的新手。
我正在使用PHP7和Mysqli进行测试,我在phpmyadmin中使用tabla1创建了一个名为prueba1的数据库。此表包含5行:
问题是,当我执行以下代码时,浏览器会在空白表中显示4行但不显示名称和ID。所有代码都在/ srv目录中的localhost中执行。
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="estilos.css" rel="stylesheet" type= "text/css">
</head>
<body background="fondos/bokeh.jpg">
<div class="caja1">
<?php
//Connecting to the DB
$link = mysqli_connect('localhost','root','test_password','prueba1')
or die('Error al conectar a la base de datos'. mysqli_connect_errno() . 'Error:'. mysqli_connect_error()."\n");
echo "<h1>Conexion Correcta a la base de datos</h1>";
$query = 'SELECT * FROM tabla1';
$result = mysqli_query($link,$query) or die("Error al ejecutar la consulta: ". mysqli_error($link));
//Checking the result
if (mysqli_num_rows($result)>0) {
//Printing the result in html
echo "<table>\n";
while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
# code...
echo "\t<tr>\n";
foreach ($line as $colvalue) {
# code...
echo "\t\t<td><h3>$col_value</h3></td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
//Cleaning memory
mysqli_free_result($result);
mysqli_close($link);
?>
</div>
</body>
</html>
提前致谢。!!
答案 0 :(得分:0)
您在while循环中工作可能是您遇到问题的原因。使用以下
while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
# code...
echo "\t<tr>\n";
echo "\t\t<td><h3>".$line['id']."</h3></td>\n";
echo "\t\t<td><h3>".$line['nombre']."</h3></td>\n";
echo "\t</tr>\n";
}