[英]How to bind variable using prepared statement
我在html中有一個表,用戶可以在其中單擊一行,並將該行的數據屬性傳遞到屏幕上加載的PHP文件。 我希望這個PHP文件包含一些與傳遞的變量相關的數據。
例如,下面是定義變量的簡單jQuery:
$(document).on('click', 'tr', function() {
var id = $(this).attr("data-recordId");
$('#section2').load('data_entry_form.php?id='+id);
});
這是PHP,旨在使用id變量並獲取關聯的元素(根據SQL查詢),其中id為fee_source_id。 PHP有什么問題? 它不起作用。 我想簡單地回顯/顯示條件fee_source_id = :id
為真的SQL查詢的結果。
<html>
<form>
<?php
require_once 'config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$id = $_GET['id'];
$sth = $dbh->prepare('
SELECT a_aif.aif_id,
a_aif.fee_source_id,
a_aif.company_name_per_sedar,
a_aif.document_filing_date
FROM a_aif
WHERE a_aif.fee_source_id = :id');
$sth->bindParam(':id', $id, PDO::PARAM_STR);
$sth->execute();
?>
</form>
</html>
您將需要獲取結果集的行。 然后回顯您需要的字段。
如果返回數組,則使用$ .getJSON而不是$('#section2')。load可能會有所幫助
$sth = $dbh->prepare('
SELECT a_aif.aif_id,
a_aif.fee_source_id,
a_aif.company_name_per_sedar,
a_aif.document_filing_date
FROM a_aif
WHERE a_aif.fee_source_id = :id');
$sth->bindParam(':id', $id, PDO::PARAM_STR);
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();
$row = $sth-> fetch(); // returns 1 row
//$data[] = $row; Returns all of array
//$id = $row['id'];// Returns indivadual fiels in array
echo $data;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.