私はあなたがMySQLを使用したいが、ここでは(テストしていません)その後、他の人のためのソリューションですしていないと述べたことを知っている:
<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
// Try to create a connection
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
// Query
$sql = 'SELECT * FROM table ';
// Get the letter selected
$letter = isset($_POST['letter']) ? $_POST['letter'] : '';
// If letter is not empty
if(!empty($letter)){
// Query continued
$sql .= 'WHERE store_name LIKE :letter';
}
// The prepared statement
$sth = $dbh->prepare($sql);
// Binding the value of the $letter to the query
if(!empty($letter)){
$sth->bindValue('letter', "$leẗter%");
}
// Executing the query
$sth->execute();
// Fetching the results
$stores = $sth->fetchAll(PDO::FETCT_OBJ);
そして、私はそうするIfElseを使用している場合は?それは便利だろうか? – tabia
私はそれ以外の場合は使用しません。つまり、それぞれの場合にif elseブロックを持つ必要があります。私はあなたが使用できるいくつかのコードで私の答えを更新します – lloiacono