CanMan2004 Posted October 5, 2006 Share Posted October 5, 2006 Hi allI have a sql database which stores peoples name in, for exampleDavid LawsonHarry BricktonI want to do a query on this table, but just with the first 2 characters. For example, if I do a search forDathen it would returnDavid Lawsonand every other row which starts with Da.There can also be numbers mixed in, for example3DThe query I use is[code]$sql = "SELECT * FROM db WHERE `firstname` = '".$firstname."' ORDER BY RAND()";[/code]But I want to check just the first 2 characters of the fieldfirstnameCan this be done with ease?RegardsDave Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/ Share on other sites More sharing options...
thepip3r Posted October 5, 2006 Share Posted October 5, 2006 $sql = "SELECT * FROM DB WHERE firstname LIKE '%Da%' DESC";Change DA in the code to whatever variable you have that holds the first two letters of each queried name. Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104297 Share on other sites More sharing options...
HuggieBear Posted October 5, 2006 Share Posted October 5, 2006 [quote author=thepip3r link=topic=110591.msg447250#msg447250 date=1160064207]$sql = "SELECT * FROM DB WHERE firstname LIKE '%Da%' DESC";[/quote]This isn't quite accurate. This will correctly identify Someone with a name of narinda, or adam or anyone with 'da' in their name.If you only want ones that start with da, then you want it like this:[code]$sql = "SELECT * FROM DB WHERE firstname LIKE 'Da%' DESC";[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104313 Share on other sites More sharing options...
thepip3r Posted October 5, 2006 Share Posted October 5, 2006 nice catch huggie. Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104318 Share on other sites More sharing options...
CanMan2004 Posted October 5, 2006 Author Share Posted October 5, 2006 That would work but im inputting a name into a html form and then submitting it, it then takes the value ive entered, and uses that. So it could beDaor it could beEddepending on what is entered, is there know way to do it by entering it the amount of characters into brackets, such as (0,2) Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104333 Share on other sites More sharing options...
roopurt18 Posted October 5, 2006 Share Posted October 5, 2006 Assume the field name in the form is [b]search[/b].[code]<?php $Clean['search'] = ValidateSearch($_POST['search']); $sql = "SELECT * FROM <table> WHERE <field> LIKE '{$Clean['search']}%'";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104337 Share on other sites More sharing options...
thepip3r Posted October 5, 2006 Share Posted October 5, 2006 [quote]Change DA in the code to whatever variable you have that holds the first two letters of each queried name. [/quote]tha'ts why you need to change "DA" in the query statement to the variable name you assign the $_POST or $_GET value passed from your HTML search form. Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104345 Share on other sites More sharing options...
CanMan2004 Posted October 5, 2006 Author Share Posted October 5, 2006 The problem is that in one of my queries searches for codes, such asE2 3ER5 2DV2 B7if I do a search forE2 6Ythen it returnsE2 6YE22 4RBE23 9Uand so on, I need to limit it, so that it just looks for the first 2 characters, as I wouldnt wantE22 4RBorE23 9UI would only wantE2 6Yreturned as it only contains 2 characters before the space..Does this explain it a little better?Any help would be great Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104356 Share on other sites More sharing options...
CanMan2004 Posted October 5, 2006 Author Share Posted October 5, 2006 Or would I need a whole new query to do that one? Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104358 Share on other sites More sharing options...
roopurt18 Posted October 5, 2006 Share Posted October 5, 2006 Are you saying you only want it to accept the first two characters of what the user entered and only return entries that have those two characters followed by a space and then followed by other text? Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104362 Share on other sites More sharing options...
thepip3r Posted October 5, 2006 Share Posted October 5, 2006 you MIGHT be able to do this in SQL, I'm not sure because i'm more versed in PHP than I am in SQL but how I would accomplish what you were looking for is leaving the query statement as is. Write all values that you get from the SQL query to an array then run each value through a loop and a conditional to check for the specific cases you're looking for. Unfortunately it doesn't look like your database information is standardized and becaues of it, you could get varying results.... hopefully a SQL guru will look at this and give you a much simpler solution. Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104363 Share on other sites More sharing options...
CanMan2004 Posted October 5, 2006 Author Share Posted October 5, 2006 yes, so that it only finds rows with 2 characters before the space and then anything following that.It might want to chance it from 2 to 3 characters, maybe 4 tooThanks Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104365 Share on other sites More sharing options...
roopurt18 Posted October 5, 2006 Share Posted October 5, 2006 How are you determining how many characters to search before the space? Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104371 Share on other sites More sharing options...
roopurt18 Posted October 5, 2006 Share Posted October 5, 2006 Two assumptions:$limit holds the number of chars to search before the spaceThe form field with the search text is named 'search'.[code]<?php$Clean['search'] = ValidateSearch($_POST['search']);$Clean['search'] = substr($Clean['search'], 0, $limit) // Get first $limit chars$sql = "SELECT ... WHERE <field> LIKE '{$Clean['search']} %'";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23083-simple-query-statement/#findComment-104386 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.