PHP脚本不返回查询,但MySql查询有效

时间:2014-05-05 01:50:45

标签: javascript php mysql ajax

我的最终目标是让一个js脚本将PHP查询的结果作为json返回,这样我就可以用它来做坏事了。

我有我想要使用的MySql查询,当我在Workbench中测试它时它肯定有效,但是当我在PHP脚本中尝试它时,什么都没有返回。 PHP中的当前查询只是一个测试数据交换的占位符。

我手动向数组中添加了一些内容(searchResults)并返回,但执行时,NOTHING来自PHP脚本。

我也明白这可能不是最安全或最有效的代码,我只想让它在这一点上运作。

<?php
    // Include your database creds and login to the db
    require_once 'login_karavites.php';
    $db = mysqli_connect($db_hostname, $db_username, $db_password);

    // Handle the input/request.
    $searchString_UNSAFE = $_POST['eName']; // change that, obviously

    // Bare minimum sanitation to prevent injection.
    $searchString = $db->escape_string($searchString_UNSAFE);

    // Construct the SQL query
    $sql = "SELECT * FROM `Halls` WHERE hall_name = 'Rose Ballroom'";

    // Do the database lookup.
    $result = $db->query($sql);

    // Create empty array to hold our results (to be sent back to the browser).
    $searchResults = array();
    $searchResults[]="wow";
    // If we had results, put them into that array
    if ($result->num_rows > 0) {

        // This loop will retrieve every row from that result set
        while ($row = $result->fetch_assoc()) {

            // From each row, just take the 'event_name' field.
            $searchResults[] = $row['hall_name'];

        }

    }

    // Done with the db, now we just have to send the results back to the browser.
    $db->close();

    // Send the correct content-type header.
    // This ensures that jQuery automatically converts the response into an 
    // array or object, rather than just treating it like a block of text.
    // Must be the FIRST thing the PHP script outputs, or it will choke.
    header('Content-type: application/json');

    // Output the data.
    echo json_encode($searchResults);

?>

js脚本。

$(document).ready(function() {
    // All this stuff runs as soon as the page is fully loaded

    // Attach a function to the Submit action on #eventForm
    $('#eventForm').submit(function() {

        // Submit the form via AJAX
        $(this).ajaxSubmit({

            // Attach a function to the "the PHP script returned some results" event
            success: function(response, status, xhr, $form){
                // I am assuming that this is your data format, for example:
                // { "searchResults": [ "result1", "result2", "result3" ] }
                // I am also assuming that you want your results in div#results
                $('div#results').html(""); // Clear it out of anything that's already there.
                console.log(response);
                for (i in response['searchResults']) {
                    $('div#results').append( response['searchResults'][i] );
                }
            },

            // Give up if PHP doesn't answer in 3 seconds
            timeout: 3000,

            // Path to the PHP file we want to send this to
            url: 'phpdata/eventsData.php'
        });

        // Make sure the browser does NOT proceed to submit the form again,
        // the old fashioned way (full page reload).
        return false;
    });
});

1 个答案:

答案 0 :(得分:0)

我的问题是:

  • 我愚蠢地从未在原始表单中设置表单方法,因此POST实际上并没有通过。

  • 我在PHP文件中设置了错误的MySql连接。

我自己的笔记,请确认您检查所有设置。

相关问题