嘿我的PHP代码返回ajax只返回一行,欢迎任何帮助。 PHP
<?php
include "db.php";
$letter=$_POST["letter"];
$qry = "SELECT * FROM book WHERE title like '$letter%'";
$res = mysql_query($qry);
$num_row = mysql_num_rows($res);
$row = mysql_fetch_assoc($res);
if($num_row>0){
while($row=mysql_fetch_array($res)){
echo "<tr><td>$row[title]</td><td><a href=$row[author]>$row[author]</a></td><td>$row[year]</td></tr></li>";
}
}else{
echo "No Books Found Begining with the letter $letter";
}
?>
的Ajax / JavaScript的
$(document).ready(function () {
$("#button").click(function(){
var letter=$("#letteroption").val();
alert(letter);
console.log(letter);
$.ajax({
type:"post",
url:"azlistscript.php",
data:"letter="+letter,
success:function(data){
//var bookdata.html(data);
$("#bookdata").append(data);
$("#search").val("");
}
});
});
});
HTML
<table id="bookdata">
<thead>
<th class="tbl_left_column">Title</th>
<th class="tbl_left_column">Author</th>
<th class="tbl_left_column">Year</th>
</thead>
</table>
数据显示正确,但是当我以字母H开头时,它只是在数据库中没有显示多行。所有愚蠢的错误都会被注意到,谢谢。
答案 0 :(得分:1)
在判断之前删除该行。
$row = mysql_fetch_assoc($res);
使用它:
echo "<tr><td>".$row['title']."</td>
<td><a href='".$row['author']."'>". $row['author']."</a></td>
<td>".$row['year']."</td>
</tr>";
编辑:
if(mysql_num_rows($res) == 0)
{
echo "No Books Found Begining with the letter $letter";
}
else{
while($row=mysql_fetch_array($res)){
.......
......
}
EDIT2:我想我理解你了。那么你需要与第一个字母进行比较,而不是像LIKE:
试试这个:
$qry = "SELECT * FROM book WHERE LEFT(title, 1) = '$letter'";
答案 1 :(得分:0)
尝试连接数据之前连接数据:
if($num_row>0){
$response = '';
while($row=mysql_fetch_array($res)){
$response.="<tr><td>$row[title]</td><td><a href=$row[author]>$row[author]</a></td><td>$row[year]</td></tr></li>";
}
echo $response;
}