考慮以下問題,我正在嘗試將Ajax
建立到PHP/MySQL
的自定義查詢。請選擇...從...帶有Ajax的地方
Javascript代碼:
var i=2;
fetchFromDBPHP("name", "tblperson", "id="+i);
function fetchFromDBPHP(column, table, condition) {
$.ajax({
type: "post",
url: "./php/fetchFromDB.php",
dataType: 'json',
data: { column: column, table: table, condition: condition },
success: function(data) {
console.log("Fetched data from PHP:" + data);
},
error:function(request, status, error) {
console.log("Reading PHP database went wrong. Error " + request.responseText);
}
});
}
PHP代碼:
<?php
$column = $_POST['column'];
$table = $_POST['table'];
$condition = $_POST['condition'];
if (isset($table)) {
$sql = "SELECT " . intval($_POST['column']) . " FROM " . intval($_POST['table']) . " WHERE " . intval($_POST['condition']);
$con = mysqli_connect("localhost", "root", "bloh", "blah");
if (!$con) {
die("Connection failed: " . mysqli_error($con));
}
$result = mysqli_query($con, $sql);
$to_encode = array();
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
mysqli_close($con);
}
?>
我的表可以是類似的:
表tblperson
:
id name firstname tel
1 Dalton Jack 555-5555
2 Smith John 555-6666
3 Doe John 555-7777
我想要做的就是類似的查詢發送給
SELECT something FROM mytable WHERE condition=that
使用Ajax。 我有一個錯誤消息,並沒有檢索到任何東西。
爲什麼你使用'intval()'? –
我試過沒有,但我仍然有我的錯誤消息。我同意,這不是必需的。 – EricF
你有沒有檢查你的網絡標籤? –