PHP脚本不返回查询,但MySql查询工作
PHP script not returning query, but the MySql query works
我的最终目标是有一个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连接错误。
我自己的笔记,确保你检查所有的设置。
相关文章:
- 在node.js-mysql中执行UPDATE查询不起作用
- 更新从Mysql查询检索到的数据?Node.js节点mysql
- 浓缩一系列mySQL查询
- 如何翻译MySQL查询's ResultSet转换为整数
- 如何在mysql查询(NODE.js)中重复使用一个参数
- onclick->mysql查询->javascript;同一页
- 向更新mysql查询传递一个id
- 如何在下拉列表更改时自动填充mysql查询结果中的文本框值
- mysql查询一次在单个查询中的多个表中插入记录
- 添加行并运行多个mySQL查询
- 如何将mysql查询的结果获取到html文本框中
- 数组数据排序类似mysql查询
- 使用jquery显示mysql查询结果
- PHP MySQL 在一个页面和一个查询中插入具有多行的数据
- 在“onexit”或“onsubmit”之后,查询mysql并填充表单字段,而无需离开页面php js
- 使用下拉列表中的值查询 MySQL 数据库,然后使用结果填充文本字段而不刷新页面
- 通过JavaScript中的PHP查询MySQL,时间间隔
- Highcharts标签格式化器查询mysql
- 限制PHP查询mysql数据库的非空行
- 使用Node.JS查询MySQL,并在网页中显示结果