我想用来自实际 MySQL 数据库表的值填充我的网页上的jQWidgets 列表框控件(当页面完成加载和呈现时)。
部分解决方案: 这里
新问题: 我已经更新了源代码,如果我硬编码 SQL 字符串 - 列表框会被填充。但我想制作一个小的 JS 函数 - popList(field, table) - 当您想要在页面上生成一个带有 MySQL 数据库值的 jQWidgets 列表框时,可以调用它。
问题是 - 由于某种原因,在执行 PHP 脚本时$field
and$table
是空的,并且我收到You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM' at line 1
错误。是什么赋予了?
页面:
<div id="ListBox">
<script type="text/javascript">
popList("name", "categories");
</script>
</div>
弹出列表(字段,值):
function popList(field, table) {
$.ajax({
type: "GET",
url: 'getListOfValues.php',
data: 'field='+escape(field)+'&table='+escape(table),
dataType: 'json',
success: function(response) {
var source = $.parseJSON(response);
$("#ListBox").jqxListBox({ source: source, checkboxes: true, width: '400px', height: '150px', theme: 'summer'});
},
error: function() {
alert('sources unavailable');
}
});
}
getListOfValues.php:
<?php
require "dbinfo.php";
// Opens a connection to a MySQL server
$connection=mysql_connect($host, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$field = $_GET["field"];
$table = $_GET["table"];
$field = mysql_real_escape_string($field);
$table = mysql_real_escape_string($table);
$qryString = "SELECT " . $field . " FROM " . $table;
$qryResult = mysql_query($qryString) or die(mysql_error());
$source = array();
while ($row = mysql_fetch_array($qryResult)){
array_push($source, $row[$field]);
}
mysql_close($connection);
echo json_encode($source);
?>