dbworkへのアクセスサンプル
<?php
try
{
$dbs = "mysql:host=db-00-mysql.cxxuvotnsxb3.ap-southeast-1.rds.amazonaws.com;dbname=dbwork;charset=utf8";
$user='admin';
$password='Train#2784';
$dbh=new PDO($dbs, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql='SELECT * FROM employee';
$stmt=$dbh->prepare($sql);
$dbh=null;
//実行
$stmt->execute();
$all = $stmt->fetchAll();
//配列を表示
echo '<table class="sample_table">';
foreach($all as $loop){
echo '<tr>';
echo '<td>'.$loop['e_id']."</td><td>".$loop['name'].'</td>';
echo '</tr>';
}
echo '</table>';
}
catch (Exception $e)
{
print '接続失敗';
exit();
}
?>
Ajaxの実現
1 HTML
<p>名前(一部)を入力 : <input type="text" id="onamae" name="onamae" />
<button id="send">送信</button></p>
<div id="return"></div>
2.JacaScript(Jquery)
<script>
jQuery(function ($) {
$("#send").on("click", function(event){
let onamae = $("#onamae").val();
console.log("----------");
console.log(onamae);
console.log("----------");
$.ajax({
type: "POST",
url: "http://13.212.86.37/sample.php",
data: { "onamae" : onamae },
dataType : "json"
}).done(function(data){
$("#return").html(data);
}).fail(function(XMLHttpRequest, status, e){
alert(e);
});
});
});
</script>
3.PHP
<?php
$onamae = $_POST['onamae'];
header("Content-type: application/json; charset=UTF-8");
try
{
$dbs = "mysql:host=db-00-mysql.cxxuvotnsxb3.ap-southeast-1.rds.amazonaws.com;dbname=dbwork;charset=utf8";
$user='admin';
$password='Train#2784';
$dbh=new PDO($dbs, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql='SELECT * FROM employee where name like :value ';
$stmt=$dbh->prepare($sql);
$stmt->bindValue(":value", '%'. $onamae .'%', PDO::PARAM_STR);
//実行
$stmt->execute();
$all = $stmt->fetchAll();
$dbh=null;
//HTML-TABLE作成
$ret_str = '<table class="sample_table">';
foreach($all as $loop){
$ret_str .= '<tr>';
$ret_str .= '<td>'.$loop['e_id']."</td><td>".$loop['name'].'</td>';
$ret_str .= '</tr>';
}
$ret_str .= '</table><br>KEY = '.$onamae ;
// $ret_str = '接続完了';
echo json_encode($ret_str);
exit;
}
catch (Exception $e)
{
$ret_str = '接続失敗';
echo json_encode($ret_str);
exit;
}
?>
ちなみにプラグイン