php 如何使用 jquery 和 ajax 查询 mysql 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10557165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:27:46  来源:igfitidea点击:

How can I query a mysql database using jquery and ajax?

phpmysqlajaxjquery

提问by anaximander

I have a form with a "select" box. When the box is selected, I want the appropriate records from the database to be shown on the same page.

我有一个带有“选择”框的表单。选中该框后,我希望数据库中的相应记录显示在同一页面上。

There are two files involved: an HTML page that contains the form:

涉及两个文件:一个包含表单的 HTML 页面:

<form id="theForm">
<select id="theDropdown">
<option value="biology">Biology</option>
<option value="chemistry">Chemistry</option>
<option value="english">English</option>
</select>
</form>
<div id="resultsGoHere"></div>

and also contains the jQuery code:

并且还包含 jQuery 代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    $('#theDropdown').on('change', function() {
       var qString = '{sub: ' + $('#theDropdown option:selected').text() + '}';
       $.post('sub_db_handler.php', qString, processResponse);
       // $('#resultsGoHere').html(qString);
    });

    function processResponse(data) {
        $('#resultsGoHere').html(data);
    }

});
</script>

The jQuery code seems to successfully grab the selected value of the select menu and formats a JSON query string, which is printed out if the commented line above is uncommented.

jQuery 代码似乎成功地获取了选择菜单的选定值并格式化了一个 JSON 查询字符串,如果上面的注释行未注释,则会打印出来。

Here is the PHP script that is referred to in the postcommand above.

这是post上面命令中引用的 PHP 脚本。

<?php

$con = mysql_connect("localhost","rongilmo_ron","******");
if(!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("rongilmo_databases", $con);
$sub = $_POST['theDropdown'];

$q = "SELECT dbs.db_name, dbs.db_url FROM dbs, subjects, subjects_databases
WHERE subjects.subject_id=subjects_databases.subject_id
AND subjects_databases.database_id=dbs.db_id
AND subjects.subject_name='$sub'";

$r = mysql_query($q);
$array = mysql_fetch_row($r);
echo json_encode($array);

?>

I get no results at all. I've tested the query in non-ajax mode, so that isn't the problem.

我根本没有得到任何结果。我已经在非 ajax 模式下测试了查询,所以这不是问题。

Still new to ajax. I've been working on this for two days and can't seem to make it work, despite reading lots of tutorials and doing a lot of googling.

ajax还是新手。我已经为此工作了两天,尽管阅读了大量教程并进行了大量谷歌搜索,但似乎无法使其发挥作用。

Any help you can offer will be greatly appreciated.

您可以提供的任何帮助将不胜感激。

回答by Jrod

Give your select a name attribute.

给您的选择一个名称属性。

<form id="theForm">
    <select id="theDropdown" name="theDropdown">
        <option value="biology">Biology</option>
        <option value="chemistry">Chemistry</option>
        <option value="english">English</option>
    </select>
</form>
<div id="resultsGoHere"></div>

Update.PHP is expecting a query string in the standard URL-encoded notation not JSON. By sending it the following way the $_POST array should contain 'sub' with the value of the selected option.

更新。PHP 期望使用标准 URL 编码表示法而不是 JSON 的查询字符串。通过以下方式发送它,$_POST 数组应包含带有所选选项值的“sub”。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    $('#theDropdown').change(function() {   
        var qString = 'sub=' +$(this).val();
        $.post('sub_db_handler.php', qString, processResponse);
    });

    function processResponse(data) {
        $('#resultsGoHere').html(data);
    }

});
</script>

<?php

    $con = mysql_connect("localhost","rongilmo_ron","******");
    if(!$con) { die('Could not connect: ' . mysql_error()); }
    mysql_select_db("rongilmo_databases", $con);
    $sub = $_POST['sub'];

    $q = "SELECT dbs.db_name, dbs.db_url FROM dbs, subjects, subjects_databases
    WHERE subjects.subject_id=subjects_databases.subject_id
    AND subjects_databases.database_id=dbs.db_id
    AND subjects.subject_name='$sub'";

    $r = mysql_query($q);
    $array = mysql_fetch_row($r);
    echo json_encode($array);

?>

回答by Elliott

You should use the ajax $.getJson() to return a json string. It will require you to use $each on the json string. See the jquery document on the website http://api.jquery.com/jQuery.getJSON/

您应该使用 ajax $.getJson() 返回一个 json 字符串。它将要求您在 json 字符串上使用 $each 。查看网站http://api.jquery.com/jQuery.getJSON/上的jquery文档