How to have multiple search keys

I have a database wherein the files are named abcd100.00b, abcd101.00b,… i need a code where wen the user enters abcd separate then 100 to 110 . all the files with the name abcd and in the range 100 to 110 should get displayed now the following code can display pnly the first four characters how do i implement this???

[php]<?php

//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET[‘keyname’]);

//check whether the name parsed is empty
if($searchTerm == “rinex_file”)
{
echo “Enter name you are searching for.”;
exit();
}
if($searchTerm == “rinex_file”)
{
echo “Enter name you are searching for.”;
exit();
}

//database connection info
$host = “localhost”; //server
$db = “rinex”; //database name
$user = “m”; //dabases user name
$pwd = “c”; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = “SELECT * FROM rinexo WHERE rinex_file LIKE ‘%$searchTerm%’”;

$results = mysqli_query($link, $query);

/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1){
echo ’

'; while($row = mysqli_fetch_array($results)){

echo ’

';

}
echo ‘

rinex version program date maker name maker number observer agency position_X_Y_Z delta wavelength rinex file
'.$row['rinex_version'].' '.$row['pgm'].' '.$row['date'].' '.$row['marker_name'].' '.$row['marker_no'].' '.$row['observer'].' '.$row['agency'].' '.$row['position_X_Y_Z'].' '.$row['delta'].' '.$row['wavelenth'].' '.$row['rinex_file'].'
’;

}else{

echo "There was no matching record for the name " . $searchTerm;

}[/php]

Something like this should work, note that I’m not comfortable with mysqli so there might be some errors, havent tested this :slight_smile:

[php]<?php

// you should create to/from fields in the form for the user to input numbers
$searchTerm = !empty($_GET[‘keyname’]) ? trim($_GET[‘keyname’]) : ‘’;
$searchFrom = !empty($_GET[‘from’]) ? (int) $_GET[‘from’] : 0;
$searchTo = !empty($_GET[‘to’]) ? (int) $_GET[‘to’] : 0;

$range = range($searchFrom, $searchTo);

$mysqli = new mysqli(“localhost”, “my_user”, “my_password”, “db_name”);
$stmt = $mysqli->prepare(‘SELECT * FROM rinexo WHERE rinex_file IN (’. trim(str_repeat(’?,’, count($range)), ‘,’) .’)’);

foreach ($range as $number) {
$value = $searchTerm . $number . ‘.00b’;
$stmt->bind_param(“s”, $value);
}

$rows = $stmt->execute();
while ($data = $rows->fetch_assoc()) {
var_dump($data);
echo ‘


’;
}[/php]

The point is to make a query that looks like this:
[php]SELECT * FROM rinexo WHERE rinex_file IN (?,?,?,?,?,?,?,?,?,?,?)[/php]

And then bind the variables:
[php]array(11) {
[0]=>
string(11) “abcd100.00b”
[1]=>
string(11) “abcd101.00b”
[2]=>
string(11) “abcd102.00b”
[3]=>
string(11) “abcd103.00b”
[4]=>
string(11) “abcd104.00b”
[5]=>
string(11) “abcd105.00b”
[6]=>
string(11) “abcd106.00b”
[7]=>
string(11) “abcd107.00b”
[8]=>
string(11) “abcd108.00b”
[9]=>
string(11) “abcd109.00b”
[10]=>
&string(11) “abcd110.00b”
}[/php]

I didn’t think you could use php functions in a query, did that change for mysqli?

I’m just concatinating the result of the function(s) into the query. The functions will return a string like this: ‘?,?,?,?’

Sponsor our Newsletter | Privacy Policy | Terms of Service