vbcoach Posted March 10, 2012 Share Posted March 10, 2012 Hello. This one is driving me up the wall. It used to work, I have a sports league database (MsSql) and one of the tables contains team Captain's first name, last name, etc.. To create a unique but simple login, the query searches for the captain's first initial of their first name + lastname + league. However since there is the remote possibility of two similar usernames (i.e. David Smith) if the last name is found, the query adds an integer to the end of the last name. So the result would look like this: DSmithM4A At the moment, my database is completely blank. However what is happening when I run the query below is I get: DSmith1M4A Can someone figure out why this code is not working correctly? If there is not more than one "Smith" no integer is supposed to be returned. There is something in the "Captain Count" that I think is not working correctly. /* * Gather captian information */ $strCaptainFirstInitial = substr($arrPost['cpt_first'],0,1); $strCaptainLastName = preg_replace("/[^a-zA-Z]/","",$arrPost['cpt_last']); $strCaptainName = strtolower($strCaptainFirstInitial.$strCaptainLastName); $sqlCaptainSearch = sprintf ( $strCaptainSearch, $strCaptainName ); $resCaptainSearch = mssql_query($sqlCaptainSearch,$conDB); /* * Create unique captain login and Password */ $strCaptainCount = (mssql_num_rows($resCaptainSearch) > 0) ? mssql_num_rows($resCaptainSearch) : "" ; $strLeagueTypeInitial = substr($arrLeagueSearch['type'],0,1); $strCaptainLogin = $strCaptainName . $strCaptainCount . $strLeagueTypeInitial . $arrLeagueSearch['size'] . $arrLeagueSearch['division']; Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/ Share on other sites More sharing options...
AyKay47 Posted March 10, 2012 Share Posted March 10, 2012 Obviously the number of rows being grabbed is 1. If this is not what you expect to be returned, then something is wrong with your query. It's hard to help any further without actually seeing the query. What data does the user have to enter in order to login? Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1325888 Share on other sites More sharing options...
vbcoach Posted March 10, 2012 Author Share Posted March 10, 2012 Well here's the thing. This query "$strCaptainCount = (mssql_num_rows($resCaptainSearch) > 0)" should basically return a null value if no captain last name is found. Like I said, the database is currently completely empty and void of any captain entries, so it should not be returning anything. So I think there may be an issue with this statement. And I posted what the query is. Anyone have any thoughts? This same statement used to work just fine. Now that I have truncated all tables, it's not working. Thoughts anyone? Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1325989 Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2012 Share Posted March 10, 2012 I don't see any query statement. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326013 Share on other sites More sharing options...
vbcoach Posted March 11, 2012 Author Share Posted March 11, 2012 You do see the code above right? Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326043 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2012 Share Posted March 11, 2012 The sql statement that is defined in $strCaptainSearch is not shown in that code. If two different people tell you something is not in your post, you should probably believe them. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326047 Share on other sites More sharing options...
vbcoach Posted March 11, 2012 Author Share Posted March 11, 2012 I am really not trying to be a jerk here. The query itself is $strCaptainSearch = "SELECT COUNT(*) FROM captain WHERE username LIKE '%s%%'"; However I did not feel that this part of the code was revelant. My apologies. Does this help now? Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326050 Share on other sites More sharing options...
AyKay47 Posted March 11, 2012 Share Posted March 11, 2012 I am really not trying to be a jerk here. The query itself is $strCaptainSearch = "SELECT COUNT(*) FROM captain WHERE username LIKE '%s%%'"; However I did not feel that this part of the code was revelant. My apologies. Does this help now? It is absolutely relevant, using a format modifier inside of a LIKE statement will often produce unexpected results. Try this: $sqlCaptainSearch = sprintf ( "SELECT COUNT(*) FROM captain WHERE username LIKE '%s'", "%" . $strCaptainName . "%" ); I'm actually surprised that your original query didn't throw an error. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326053 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2012 Share Posted March 11, 2012 A COUNT query will always return ONE FRICKEN ROW, unless the query failed due to an error of some kind. You must retrieve the row's data and access the count value in order to determine how many matching rows the query found. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326054 Share on other sites More sharing options...
vbcoach Posted March 11, 2012 Author Share Posted March 11, 2012 Error: Notice: Undefined variable: strCaptainName in E:\Web Server\baltimorebeach_com\htdocs\registration\verify.php on line 34 However the username now did not have the integer. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326060 Share on other sites More sharing options...
AyKay47 Posted March 11, 2012 Share Posted March 11, 2012 Error: Notice: Undefined variable: strCaptainName in E:\Web Server\baltimorebeach_com\htdocs\registration\verify.php on line 34 However the username now did not have the integer. this error is self explanatory.. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326062 Share on other sites More sharing options...
vbcoach Posted March 11, 2012 Author Share Posted March 11, 2012 Well my friend, the error is coming from the code YOU gave me. So I am asking you: why this error? Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326151 Share on other sites More sharing options...
xyph Posted March 11, 2012 Share Posted March 11, 2012 What part of 'undefined variable' do you not understand? Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326158 Share on other sites More sharing options...
AyKay47 Posted March 11, 2012 Share Posted March 11, 2012 Well my friend, the error is coming from the code YOU gave me. So I am asking you: why this error? It has nothing to do with the code that i gave you, it has to do with where you put it. Quote Link to comment https://forums.phpfreaks.com/topic/258632-php-query-always-returns-a-result-as-interger/#findComment-1326170 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.