.使用PHP发布javascript以启用select语句返回
.使用PHP发布javascript以启用select语句返回 好的,我有这个脚本,它正在运行.使用PHP发布javascript以启用select语句返回,javascript,php,mysql,Javascript,Php,Mysql,.使用PHP发布javascript以启用select语句返回 好的,我有这个脚本,它正在运行 $.post('2.php', { id: 12345 }, function(data) { // Increment vote count, etc }); 这就是我的2.php的样子 $data = $_POST['id']; $file = fopen("test.txt","w"); echo fwrite($file, $data); fclose($file); 所以我做了
$.post('2.php', { id: 12345 }, function(data) {
// Increment vote count, etc
});
这就是我的2.php的样子
$data = $_POST['id'];
$file = fopen("test.txt","w");
echo fwrite($file, $data);
fclose($file);
所以我做了一个测试,运行1.php,看到test.txt是用数据创建的
这证明连接是成功的
现在是困难的部分
我需要将id:12345发送到2.php,2.php需要
"select * from account where account_id='$data'";
然后是返回结果,我考虑使用MYSQL\u ASSOC或MYSQL\u两者
我不知道哪一个最好
然后得到结果,无论是一行结果还是多行结果
以数组形式返回,然后使用1.php执行
alert( ArrayReturnResult );
假设我的account表具有此值
account_id, account_name, account_phone, account_email
如何实现这一点?假设您知道如何建立数据库连接(当然是使用PDO),您可以在2.php中执行以下操作:
if(!empty($_POST)) {
$data = (int) $_POST['id'];
// query the table
$stmt = $pdo->prepare("SELECT * FROM account WHERE account_id = :id");
$stmt->bindValue(":id", $data, PDO::PARAM_INT);
$stmt->execute();
// fetch results
$buffer = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$buffer[] = $row;
}
// output JSON string
echo json_encode($buffer);
}
当然,这没有经过测试。。。如果处理个人信息,可能不安全
不要忘记更新您的$。post
方法,以便它可以使用JSON编码的数据:
$.post('2.php', { id: 12345 }, function(data) {
console.log(data); // this will now be a JS object, from 2.php
}, 'json');
假设您知道如何建立数据库连接(当然是使用PDO),您可以在2.php中执行类似的操作:
if(!empty($_POST)) {
$data = (int) $_POST['id'];
// query the table
$stmt = $pdo->prepare("SELECT * FROM account WHERE account_id = :id");
$stmt->bindValue(":id", $data, PDO::PARAM_INT);
$stmt->execute();
// fetch results
$buffer = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$buffer[] = $row;
}
// output JSON string
echo json_encode($buffer);
}
当然,这没有经过测试。。。如果处理个人信息,可能不安全
不要忘记更新您的$。post
方法,以便它可以使用JSON编码的数据:
$.post('2.php', { id: 12345 }, function(data) {
console.log(data); // this will now be a JS object, from 2.php
}, 'json');
假设您知道如何建立数据库连接(当然是使用PDO),您可以在2.php中执行类似的操作:
if(!empty($_POST)) {
$data = (int) $_POST['id'];
// query the table
$stmt = $pdo->prepare("SELECT * FROM account WHERE account_id = :id");
$stmt->bindValue(":id", $data, PDO::PARAM_INT);
$stmt->execute();
// fetch results
$buffer = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$buffer[] = $row;
}
// output JSON string
echo json_encode($buffer);
}
当然,这没有经过测试。。。如果处理个人信息,可能不安全
不要忘记更新您的$。post
方法,以便它可以使用JSON编码的数据:
$.post('2.php', { id: 12345 }, function(data) {
console.log(data); // this will now be a JS object, from 2.php
}, 'json');
假设您知道如何建立数据库连接(当然是使用PDO),您可以在2.php中执行类似的操作:
if(!empty($_POST)) {
$data = (int) $_POST['id'];
// query the table
$stmt = $pdo->prepare("SELECT * FROM account WHERE account_id = :id");
$stmt->bindValue(":id", $data, PDO::PARAM_INT);
$stmt->execute();
// fetch results
$buffer = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$buffer[] = $row;
}
// output JSON string
echo json_encode($buffer);
}
当然,这没有经过测试。。。如果处理个人信息,可能不安全
不要忘记更新您的$。post
方法,以便它可以使用JSON编码的数据:
$.post('2.php', { id: 12345 }, function(data) {
console.log(data); // this will now be a JS object, from 2.php
}, 'json');
你需要什么帮助?连接/查询数据库,即。
新建PDO(…)/new mysqli(…)
和->查询(“选择…”)
;获取结果,即,while($row=fetch())…
;或者将结果发送回$.post()
,即echo json(encode($results))
?现在你的问题不清楚,我的问题太宽泛了。你需要什么帮助?连接/查询数据库,即。新建PDO(…)/new mysqli(…)
和->查询(“选择…”)
;获取结果,即,while($row=fetch())…
;或者将结果发送回$.post()
,即echo json(encode($results))
?现在你的问题不清楚,我的问题太宽泛了。你需要什么帮助?连接/查询数据库,即。新建PDO(…)/new mysqli(…)
和->查询(“选择…”)
;获取结果,即,while($row=fetch())…
;或者将结果发送回$.post()
,即echo json(encode($results))
?现在你的问题不清楚,我的问题太宽泛了。你需要什么帮助?连接/查询数据库,即。新建PDO(…)/new mysqli(…)
和->查询(“选择…”)
;获取结果,即,while($row=fetch())…
;或者将结果发送回$.post()
,即echo json(encode($results))
?现在你的问题不清楚,我的问题可能太宽泛了。