<?php
function h($str) {
return htmlspecialchars($str, ENT_QUOTES|ENT_HTML5, "UTF-8");
}
function _h( $inputs ) {
if ( is_array( $inputs ) ) {
$_input = array();
foreach ( $inputs as $key => $val ) {
if ( is_array( $val ) ) {
$key = htmlspecialchars( $key, ENT_QUOTES, 'UTF-8' );
$_input[ $key ] = _h( $val );
} else {
$key = htmlspecialchars( $key, ENT_QUOTES, 'UTF-8' );
$_input[ $key ] = htmlspecialchars( $val, ENT_QUOTES, 'UTF-8' );
}
}
return $_input;
} else {
return htmlspecialchars( $inputs, ENT_QUOTES, 'UTF-8' );
}
}
function insertData($host, $db_name, $db_user, $db_pass, $db_tablename, $insert_data) {
try {
$conn = new PDO("mysql:host=$host;dbname=$db_name", $db_user, $db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$keys = array_keys($insert_data);
$values = array_values($insert_data);
$num_values = count($values);
$placeholders = implode(',', array_fill(0, $num_values, '?'));
$stmt = $conn->prepare("INSERT INTO $db_tablename (" . implode(',', $keys) . ") VALUES ($placeholders)");
$stmt->execute($values);
$conn = null;
return true;
} catch(PDOException $e) {
return "Error deleting record: " . $e->getMessage();
}
}
function fetch_mysql_data($host, $dbname, $db_user, $password, $table_name) {
// MySQLへの接続
$pdo = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $db_user, $password);
// エラー時の例外処理
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// データの取得
$stmt = $pdo->prepare("SELECT * FROM `{$table_name}`");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function fetch_mysql_data_where($host, $dbname, $db_user, $password, $table_name, $where_sentence) {
// MySQLへの接続
$pdo = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $db_user, $password);
// エラー時の例外処理
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// データの取得
$stmt = $pdo->prepare("SELECT * FROM `{$table_name}` {$where_sentence};");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function deleteRecord($host, $dbname, $db_user, $password, $table_name, $delete_id) {
try {
$dsn = "mysql:host=$host;dbname=$dbname";
$pdo = new PDO($dsn, $db_user, $password);
$stmt = $pdo->prepare("DELETE FROM $table_name WHERE id = ?");
$stmt->execute([$delete_id]);
if ($stmt->rowCount() == 1) {
return "Record deleted successfully";
} else {
return "Record not found";
}
} catch (PDOException $e) {
return "Error deleting record: " . $e->getMessage();
}
$pdo = null;
}
?>
詳細は後程記入