使用Ajax PHP和jQuery从MySQL获取数据的问题
嗨,我已经问过这个问题了,但不幸的是我没有得到合适的答案。MySQL数据库中有两个表,分别称为Items和Item,如下所示: 我有两个.php文件作为bwlow;index.php和results.php index.php如下所示:使用Ajax PHP和jQuery从MySQL获取数据的问题,php,jquery,ajax,Php,Jquery,Ajax,嗨,我已经问过这个问题了,但不幸的是我没有得到合适的答案。MySQL数据库中有两个表,分别称为Items和Item,如下所示: 我有两个.php文件作为bwlow;index.php和results.php index.php如下所示: <html> <head></head> <body> <?php $mysqli = new mysqli('localhost', 'root', '', 'moviedb'); i
<html>
<head></head>
<body>
<?php
$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
if ($mysqli->connect_errno)
{
die('Unable to connect!');
}
else{
$query = 'SELECT * FROM tblItems';
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
?>
<p> Select a Genre
<ul>
<?php
while($row = $result->fetch_assoc()){
?>
<li><div class="selectGenre"><?php echo $row['item']; ?></div></li>
<?php
}
?>
</ul>
</p>
<p id="result"></p>
<?php
}
else
{
echo 'No records found!';
}
$result->close();
}
else
{
echo 'Error in query: $query. '.$mysqli->error;
}
}
$mysqli->close();
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('.selectGenre').click(function()
{
if($(this).html() == '') return;
$.get(
'results.php',
{ id : $(this).html() },
function(data)
{
$('#result').html(data);
}
);
});
});
</script>
</body>
</html>
results.php是:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
$resultStr = '';
$query = 'SELECT type FROM tblItem where id='.$_GET['id'];
if ($result = $mysqli->query($query))
{
if ($result->num_rows > 0)
{
$resultStr.='<ul>';
while($row = $result->fetch_assoc())
{
$resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['type'];
'</li>';
}
$resultStr.= '</ul>';
}
else
{
$resultStr = 'Nothing found';
}
}
echo $resultStr;
?>
第一个partindex.php在tblItems表的基础上填充列表,但单击列表时,不会从results.php文件向页面返回任何值,甚至不会返回错误消息。你能告诉我我做错了什么吗?你的问题是:
$query = 'SELECT type FROM tblItem where id='.$_GET['id'];
什么时候
在results.php中尝试以下查询
$sql = "SELECT tblItem.* FROM tblItems
INNER JOIN tblItem USING(id)
WHERE tblItems.item = '".$mysqli->real_escape_string($_GET['id'])."'";
同时将$row['type']更改为$row['Name'.这对您来说会更容易: 试试这个,我编辑你的index.php代码:
<html>
<head></head>
<body>
<?php
$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
if ($mysqli->connect_errno)
{
die('Unable to connect!');
}
else{
$query = 'SELECT * FROM tblItems';
if ($result = $mysqli->query($query)) {
if ($result->num_rows > 0) {
?>
<p> Select a Genre
<ul>
<?php
while($row = $result->fetch_assoc()){
?>
<li><div class="selectGenre" onclick="return printSearch('<?php echo $row['id']; ?>');"><?php echo $row['item']; ?></div></li>
<?php
}
?>
</ul>
</p>
<p id="result"></p>
<?php
}
else
{
echo 'No records found!';
}
$result->close();
}
else
{
echo 'Error in query: $query. '.$mysqli->error;
}
}
$mysqli->close();
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function printSearch(idVal)
{
$.get(
'results.php',
{ id : idVal },
function(data)
{
$('#result').html(data);
}
);
}
</script>
</body>
</html>
好的,这里是result.php
<?php
$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
$resultStr = '';
$query = 'SELECT * FROM tblItem where id='.$_GET['id'];
if ($result = $mysqli->query($query))
{
if ($result->num_rows > 0)
{
$resultStr.='<ul>';
while($row = $result->fetch_assoc())
{
$resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['Name'].
'</li>';
}
$resultStr.= '</ul>';
}
else
{
$resultStr = 'Nothing found';
}
}
echo $resultStr;
?>
你怎么知道什么都没有被退回。。。从控制台中的检查请求?您知道是否正在发出ajax请求吗?没有显示单击处理程序代码或ajax代码。如果问题与服务器代码或javascript相关,那么这将有助于缩小范围。提供尽可能多的细节Hi KeyboardSmasher,感谢您的评论,我将select子句更新为$query='select name FROM tblItem where id='。$\u GET['id'];但是我没有明白你的意思你能告诉我更多的事情吗?thanks@user1760110:KeyboardSmasher意味着,在您的tblItem中,id字段是整数而不是varchar,所以无论您选择什么,您都没有字符串id!您好,air4x,我将result.php代码更新为“$query=SELECT tblItem.*从tblItems内部使用tblItems.item='.$mysqli->real_escape_string$_GET['id'.”;if$result=$mysqli->query$query{if$result->num_rows>0{$resultStr.=;而$row=$result->fetch_assoc{$resultStr.=;$resultStr.=;;}$resultStr.=;}否则{$resultStr='Nothing';}echo$resultStr;?>`但这仍然是无所作为!谢谢Sephoy,我只是复制并粘贴了你的代码,再一次我没有在页面上得到任何东西@user1760110:只需在两个文件中复制这一点谢谢Sephoy08我只是测试了它们,它们工作得很好。只是想知道为什么我们不能在您的示例中使用jQuery document ready方法?@user1760110:这并不是说我们不能使用它。。。我只是让它更容易,但如果你想你可以玩它,并使用你喜欢的方法。
<?php
$mysqli = new mysqli('localhost', 'root', '', 'moviedb');
$resultStr = '';
$query = 'SELECT * FROM tblItem where id='.$_GET['id'];
if ($result = $mysqli->query($query))
{
if ($result->num_rows > 0)
{
$resultStr.='<ul>';
while($row = $result->fetch_assoc())
{
$resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['Name'].
'</li>';
}
$resultStr.= '</ul>';
}
else
{
$resultStr = 'Nothing found';
}
}
echo $resultStr;
?>