我需要使用.php和.html文件填充一个表,其中包含有关10个国家的数据。我已经下载了XAMPP (7.1.8),我正在使用一台运行塞拉的MacBook专业版2012 (无法让XAMPP-VM运行-启动时出现信号杀死错误信息)。
我已经在MySql中创建了数据库testdb,并对database.html文件和getuser.php文件进行了如下编码,并将它们保存到服务器上的htdocs文件夹中-但当它渲染时,它不会提取xml文件或其中的数据。
我相信问题要么出在php的46行,要么出在html的36行,但我对php的了解还不够多,不知道如何改变它才能正常工作。任何帮助都将不胜感激,
谢谢
苏泽
database.html
<head>
<script>
function showUser (str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest ();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function () {
if (this.readyState==4 && this.status==200) {
document.getElementById("hint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value ="">Select a country:</option>
<option value ="1">United Kingdom</option>
<option value="2">France</option>
<option value="3">Germany</option>
<option value="4">India</option>
<option value="5">Hungary</option>
<option value="6">Ireland</option>
<option value="7">Greece</option>
<option value="8">USA</option>
<option value="9">Japan</option>
<option value="10">Spain</option>
</select>
</form>
<br>
<div id="hint"><b>Country info will be highlighted below</b></div>
</body>
getuser.php
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding 5px;
}
th {text-align: left;}
</style>
</head>
<?php
$q = intval ($_GET['q']);
$con = mysqli_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"testdb");
$sql = "SELECT * FROM records WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table
<tr>
<th>Country</th>
<th>Capital City</th>
<th>Currency</th>
</tr>";
error_reporting(E_ERROR | E_PARSE);
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['Capital City'] . "</td>";
echo "<td>" . $row['Currency'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
发布于 2017-09-11 18:38:13
问题是你获取的是一个数组而不是关联。使用mysqli_fetch_assoc()。然后你就可以像你一样访问数组了。
while ($row = mysqli_fetch_assoc($result))
并关闭第一个表标记。
https://stackoverflow.com/questions/46151506
复制相似问题