Jump to content

PHP query always returns a result as interger


vbcoach

Recommended Posts

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'];

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.