0

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.

0

2 Answers 2

6

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.'%' ));
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Restrictions:

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.

6 Comments

problem solve with @VMai query, But i also tried your query and its not working.
$stmt = $conn->prepare("SELECT roomName, roomNo FROM roomnames WHERE MATCH (?, ?) AGAINST ('houserentsystem')"); $stmt->execute(array('%'.$searchFilter.'%','%'.$searchFilter.'%' )); not working.
@SizzlingCode Sry to hear that, I edited my answer. It's all about the query, not the PDO, Glad to hear that an answer helped you. And your title says "multiple tables", that's why I put the MATCH thing...
It should be $stmt = $conn->prepare("SELECT roomName, roomNo FROM roomnames WHERE MATCH (roomName, roomNo) AGAINST (?)");, I think. But the problem with fulltext search is that it searches words by default.
Please have a look at my suggestion: the columns to be searched go into the MATCH clause, the search term into AGAINST. We know the two columns to be sarched: roomName and roomNo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.