PHP脚本不返回查询,但MySql查询工作

PHP script not returning query, but the MySql query works

本文关键字:查询 MySql 工作 PHP 返回 脚本      更新时间:2023-09-26

我的最终目标是有一个js脚本返回PHP查询的结果作为json,所以我可以用它做邪恶的事情。

我有MySql查询,我想使用,它肯定是工作时,我在工作台测试它,但当我在PHP脚本中尝试它,没有得到返回。当前PHP中的查询只是一个占位符,用于测试数据交换。

我手动在数组(searchResults)中放入一些东西,并得到返回,但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;
    });
});

我的问题是:

  • 我愚蠢地没有在原始表单中设置form-method,所以POST实际上没有通过。

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

我自己的笔记,确保你检查所有的设置。