Jump to content

PHP Query


jaykappy

Recommended Posts

I Have a query that is working fine...but I want to create a new one that is build for a search function.

I want to search for a value in multiple fields and as a wild card (any part of the field value)

 

I have been using this query:

 

 
$query = " 
SELECT 
Id,Yr,ProjectName,Department,Owner,MaplibraryName,
Description,MaplibraryPath,PageSize,Scale,LastModified,
Notes,ModifiedBy,FullPath,Path_jpg,PDF_Path,JPG_Path
FROM tbl_maplibrary
WHERE 
Department = :dept
ORDER BY 
Yr DESC
"; 

 

How do I modify it to WHERE Department CONTAINS :dept OR Field1 CONTAINS :dept OR Field2 CONTAINS :dept

 

Any help would be greatly appreciated.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/275872-php-query/
Share on other sites

 

but you can use the '%' like

 

 

WHERE Department LIKE '%:dept%'

Thank you fooDigi....was look for the correct syntax..... 

 

So I assume I can string these together?

 

WHERE Department LIKE '%:dept%'  OR Field1 LIKE '%:dept%' OR Field2 LIKE '%:dept%'

 

Thanks for your time and resources.....

Link to comment
https://forums.phpfreaks.com/topic/275872-php-query/#findComment-1419620
Share on other sites

Based upon a couple quick searches I did, putting the parameter between percent symbols in the prepared statement does not work. The solutions I found were to either append the percent symbols to the value being passed as the parameter OR to concat the percents in the query.

 

http://stackoverflow.com/questions/1786436/php-pdo-prepared-statement-mysql-like-query

http://board.phpbuilder.com/showthread.php?10368514-RESOLVED-mysqli-gt-prepare%28-WHERE-LIKE-%29

 

Since you are using the term multiple places, I would add the symbols to the value before passing it in as a parameter

 

 

$query = "
SELECT Id,Yr,ProjectName,Department,Owner,MaplibraryName,
       Description,MaplibraryPath,PageSize,Scale,LastModified,
       Notes,ModifiedBy,FullPath,Path_jpg,PDF_Path,JPG_Path
FROM tbl_maplibrary
WHERE Department LIKE :dept
   OR Field1 LIKE :dept
   OR Field2 LIKE :dept
ORDER BY Yr DESC";

$prep = $dbh->prepare($query);
$ret = $prep->execute(array(':dept' => "%{$department}%"));
Link to comment
https://forums.phpfreaks.com/topic/275872-php-query/#findComment-1419621
Share on other sites

Thank you once again for the help..very appreciated.....trying to learn here...

 

So If I do this:

 

 

window.location.href = "/MainPageindex.php?Value=" + $("#searchvalue").val();

 

Then in my MainPageindex.php page do this:  I DONT think that I have to correct syntax around the GET Statement

 

 

 
 
$query = " 
SELECT 
Id,Yr,ProjectName,Department,Owner,MaplibraryName,
Description,MaplibraryPath,PageSize,Scale,LastModified,
Notes,ModifiedBy,FullPath,Path_jpg,PDF_Path,JPG_Path
FROM tbl_maplibrary
WHERE 
Department LIKE :Value
OR Field1 LIKE :Value
OR Field2 LIKE :Value
ORDER BY 
Yr DESC
 
 
$stmt = $db->prepare($query); 
//$stmt->execute();
$stmt->execute(array(":Value" => "% $_GET['Value'] %" ));  
Link to comment
https://forums.phpfreaks.com/topic/275872-php-query/#findComment-1419662
Share on other sites

 . . . and don't put extraneous spaces between the percent signs and the value - otherwise the data will only match on records where the search value exists with spaces on either side. Plus, I always trim such data from the user as well.

 

$searchValue = trim($_GET['Value']);
stmt->execute(array(":Value" => "%{$searchValue}%" )); 
Link to comment
https://forums.phpfreaks.com/topic/275872-php-query/#findComment-1419665
Share on other sites

fooDigi, Jessica, Psycho thank you very much...your help was very appreciated...

Credit to all 3 of you....

 

MainPageindex.php

 

 
$(".Searchbutton").click(function(){
if( !$('#searchvalue').val() ) {
alert("Please Enter a Search Value");
}
else{
alert("You are searching for: " + ($('#searchvalue').val())) ;
window.location.href = "/MainPageindexSearch.php?ItemValue=" + $("#searchvalue").val(); 
}
}); 

 

MainPageindexSearch.php

 

 

 
$query = " 
SELECT 
Id,Yr,ProjectName,Department,Owner,MaplibraryName,
Description,MaplibraryPath,PageSize,Scale,LastModified,
Notes,ModifiedBy,FullPath,Path_jpg,PDF_Path,JPG_Path
FROM tbl_maplibrary
WHERE 
Description LIKE :Values
OR MaplibraryName LIKE :Values
ORDER BY 
Yr DESC
"; 
 
$stmt = $db->prepare($query); 
$searchValue = trim($_GET['ItemValue']);
$stmt->execute(array(":Values" => "%{$searchValue}%" )); 
Link to comment
https://forums.phpfreaks.com/topic/275872-php-query/#findComment-1419684
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.