I want to use like on two columns in my table now.
tablename = roomnames
table columns = roomID
, roomName
, roomNo
i have an input field which sends a string value on which the query should execute.
Here below is the PHP PDO code
<?php
/**
* Created by PhpStorm.
* User: HaiderHassan
* Date: 9/3/14
* Time: 9:52 PM
*/
header('Access-Control-Allow-Origin: *');
try {
$conn = new PDO('mysql:host=localhost;dbname=houserentsystem;charset=utf8', 'root', 'admin');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
if($_POST['searchFilter']){
$searchFilter = $_POST['searchFilter'];
$stmt = $conn->prepare("SELECT roomName, roomNo FROM roomnames WHERE roomName LIKE ? OR roomNo LIKE ?");
$stmt->execute(array('%'.$searchFilter.'%'));
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
print_r(json_encode($results));
}
the query i tried is wrong and not working here is what i tried now.
$stmt = $conn->prepare("SELECT roomName, roomNo FROM roomnames WHERE roomName LIKE ? OR roomNo LIKE ?");
$stmt->execute(array('%'.$searchFilter.'%'));
i want if i search room No or roomName, it will show me result if that room number or room name is present in the table of database.
Sorry for my bad English. I hope i have explained. i want to search in multiple columns for a table against a input string, Input string can be a no or name. Number is for roomNo and name for room name.
You've got to have a value for every parameter, so double your searchfilter in the array of the values for the parameters:
$stmt = $conn->prepare("SELECT roomName, roomNo FROM roomnames WHERE roomName LIKE ? OR roomNo LIKE ?");
$stmt->execute(array('%'.$searchFilter.'%','%'.$searchFilter.'%' ));
You could use the MATCH
struct from MySQL:
SELECT roomName, roomNo FROM roomnames WHERE MATCH (roomName, roomNo IN BOOLEAN MODE) AGAINST(?)
The params needed inside AGAINST(?)
Would then go as an array inside $stmt->execute(...)
For further information, see here.
Full-text indexes can be used only with MyISAM tables. (In MySQL 5.6 and up, they can also be used with InnoDB tables.) Full-text indexes can be created only for CHAR, VARCHAR, or TEXT columns.
From the docs:
The
IN BOOLEAN MODE
prevents from searching only by words, but by string.