我正在创建一个包含三个组件的网站。 (我将添加JQuery和CSS,但是现在我只是想解决一些基本的机制)
我的结构是:
以下是我的代码:
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="RipListQuery.js"></script>
<title>CD Riplist Query</title>
</head>
<body onload="getArtists()">
<h1>CD Riplist Query</h1>
<div id="divArtists" >
</div>
</body>
</html>
使用Javascript:
function getArtists() {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","dataAccess.php",false);
xmlhttp.send();
document.getElementById("divArtists").innerHTML=xmlhttp.responseText;
}
PHP:
<?php
$con = mysql_connect('myServerAddress', 'ainsworthremote', 'myPassword');
if (!$con) {
die("Could not connect: " . mysql_error());
}
$response =
"<table border='1' id='Artists'><thead><tr><th>Artist</th><th>Albums</th></tr></thead><tbody>";
mysql_select_db("ainsworthremote", $con);
$sql="SELECT artist, COUNT(*) AS albums FROM CD_RipList GROUP BY artist ORDER BY artist ";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$response = $response . "<tr>" .
"<td>" . $row['artist'] . "</td>" .
"<td>" . $row['albums'] . "</td>" .
"</tr>";
}
$response = $response . "</tbody></table>";
mysql_close($con);
echo $response;
?>
我得到以下内容,而不是像预期的那样得到一个充满艺术家名字的表格。
CD Riplist查询
ArtistAlbums"; mysql_select_db("ainsworthremote", $con); $sql="SELECT artist, COUNT(*) AS albums FROM CD_RipList GROUP BY artist ORDER BY artist "; $result=mysql_query($sql); while ($row = mysql_fetch_array($result)) { $response = $response . "" . "" . $row['artist'] . "" . "" . $row['albums'] . "" . ""; } $response = $response . ""; mysql_close($con); echo $response; ?>
我想知道的(除了我做错了)是:我的标签发生了什么?
答案 0 :(得分:0)
你的while语句没有意义,即使它确实你应该有等于'=='的运算符而不是'='。您正在从SQL请求返回一个数组,因此您需要遍历该数组。此外,当附加到现有的str变量时,可以使用“。=”运算符,例如
$response .= "</tbody></table>";
而你的while循环可能是一个foreach循环。
$query = mysql_query($sql);
$result = mysql_fetch_array($result);
foreach($result as $row){
$response .= "<tr>" .
"<td>" . $row['artist'] . "</td>" .
"<td>" . $row['albums'] . "</td>" .
"</tr>";
}
$response .= "</tbody></table>";
echo $response;
看看是否有帮助。最重要的是,你可能会有一些报价逃避问题,虽然它看起来不错。