![]() |
Dumb SQL question
This should pick up any pattern that shows up in the search string right?
Code:
$result = mysql_query("SELECT url, title FROM fappageinfo WHERE title LIKE '%{$searchstring}%' ORDER BY dateadded DESC") or die('error database'); Code:
$result = mysql_query("SELECT url, title FROM fappageinfo WHERE title LIKE '%{$searchstring}%' OR title LIKE '{$searchstring}%' ORDER BY dateadded DESC") or die('error database'); For example title "transvestite threesome tea party" Search returns for "threesome" or "tea" but not transvestite. |
Oh yea, Here is the site.
Fap Rush - Adult Website Listing I don't have much in the database yet, only 4 or 5 sites. Search for lesbian, snake or tea should bring back results. |
You should set up the query first and then execute mysql_query($query);
eg: Code:
<?php Code:
while ($row = mysql_fetch_assoc($result)) { PHP: mysql_query - Manual |
Quote:
:thumbsup I did not know this. I am gonna go back and rewrite in mysqli. Thanks AdultKing. |
looks way better. SQLI isn't deprecated, right?
Code:
//SQL Connection |
Quote:
Are you getting the output you want now ? I checked your website and it's still not returning matches for any word in the query. |
Quote:
No, Still not working. But I want to try using parameterized SQL now. Kind of difficult to find good examples of the correct way to do things and too much old code on the internet. |
Quote:
I use Laravel now so my raw MySQL query knowhow is rusty, Laravel spoils you like that. Maybe some other coders on GFY could chime in and tell me if I've missed something fundamental, it's likely. Database: Query Builder - Laravel - The PHP Framework For Web Artisans |
transvestite returns a result for me. Also I personally like working with PDO as an alternative, no need to switch just throwing that out there as an option.
|
Quote:
|
Quote:
Thanks for your help fellas. |
Search for transvestite on your front page returns a result for me. Searching again on search.php does not. Maybe compare the two pages?
|
Sorry the two pages are the same. The problem is the extra whitespace in your input field. Remove that space and use the php trim () function to remove any extra white space from your search terms after submitting the form.
|
Me, I would try something like this:
<?php $searchstring='transvestite threesome tea party'; $terms=explode(' ',$searchstring); $conditions=array(); for($i=0;$i<count($terms);$i++) { if(!empty($terms[$i])) { $conditions[$i]="title like '%" . trim($terms[$i]) . "%'"; } } $sql_str ="select url, title from fappageinfo where "; for($i=0;$i<count($conditions);$i++) { if($i>0) { $sql_str .=" or "; } $sql_str .=$conditions[$i] . " "; } echo "sql=" . $sql_str . "<br>"; ?> the echo produces: sql=select url, title from fappageinfo where title like '%transvestite%' or title like '%threesome%' or title like '%tea%' or title like '%party%' then use whatever flavor you want to exec the query, mysql, msqli, PDO, whatever Hope that helps |
Quote:
|
I was confused by the syntax of '%{...}%' as I had never seen that before. Researched a little, saw some folks using it, could not find anything official so I set up a little test to see what would happen.
I first run the query using the routine I did above. I then run it using the original syntax. My test database looks like this: `test_table` (`id`, `url`, `title`) (1, 'test url 1', 'transvestite threesome tea party'), (2, 'test url 2', 'threesome tea party'), (3, 'test url 3', 'tea party'), (4, 'test url 4', 'party'), (5, 'test url 5', 'tea for me'), (6, 'test url 6', 'part time for transvestites'); Mo code (at http://madspiders.com/gfy_mysql_test/test.php) looks like this: Code:
<?php start of program In new code hooking up got db past connect searchstring=transvestite threesome tea party Constructing conditions Constructing sql str sql string=select id, url, title from test_table where title like '%transvestite%' or title like '%threesome%' or title like '%tea%' or title like '%party%' Sql returned 6 rows Rec 1 Title: transvestite threesome tea party matched Rec 2 Title: threesome tea party matched Rec 3 Title: tea party matched Rec 4 Title: party matched Rec 5 Title: tea for me matched Rec 6 Title: part time for transvestites matched Broke this out of html to show how the sql str is being constructed: $sql_str ="Select id, url, title from test_table "; $sql_str .="where title LIKE '%{$searchstring}%'"; As soons as we show it in php the brackets {} disappear: sql starts back here: sql=Select id, url, title from test_table where title LIKE '%transvestite threesome tea party%' when we go to execute that we get back a: Sql returned 1 rows Rec 1 Title: transvestite threesome tea party matched End of program |
Thread like this restores my faith in GFY.
Not the way I do my queries, but very smart. Will make all new queries in a similar way now... |
Quote:
<?php $searchstring='transvestite threesome tea party'; $terms=explode(' ',$searchstring); $whereclause=''; foreach($terms as $term){$whereclause .=(!empty($term)?(!empty($whereclause)?"or ":'') . "title like '%" . trim($term) . "%' ":'');} $sql_str="select url, title from fappageinfo " . (!empty($whereclause)?'where ' . $whereclause:'');; echo "sql=" . $sql_str . "<br>\n"; ?> It is exactly the same, but different. . |
Quote:
Anywho, Thanks PornWorx. And sarettah for taking the time to make that sweet SQL for me. I will get around to trying it sometime. Friends and family coming and hanging out this week, So I'm not getting anytime to play with my code. |
tl;dr: the code snippets.
It's been a little while since I've had to do any heavy lifting with MySQL, but if I recall correctly, when you use % in your database query, it assumes that you're looking for something before the beginning of the string. Therefore anything that starts with your string, and nothing before it, it will return 0 results. |
Quote:
https://dev.mysql.com/doc/refman/5.0...functions.html % matches any number of characters, even zero characters. from our examples above: sql=Select id, url, title from test_table where title LIKE '%transvestite threesome tea party%' when we go to execute that we get back a: Sql returned 1 rows Rec 1 Title: transvestite threesome tea party matched |
Quote:
This worked. Code:
$trimstring = trim($outputsearchstring, " \t\n\r\0\x0B"); Thanks again. PornWorx, AdultKind, sarettah, suesheboy, and drclockwork. For taking the time to read and offer advise in my post. I have learned a lot. |
All times are GMT -7. The time now is 12:33 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc