Testing if a value exists.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Levy

    #1

    Testing if a value exists.

    Hi

    Im trying to create a function that tests if a 'Username' exists in a
    particular table. If the value is found then the code generates 'Username1'
    as the username, and check if that username exists. If it does, it
    continues onto generating 'Username2' and so on.

    <?
    //testing with username andrew
    $name = "andrew";

    mysql_pconnect( "server", "username", "pass");
    mysql_select_db ("db");
    $result = mysql_query("SE LECT Username FROM users WHERE Username =
    '$name'");
    $num_rows = mysql_num_rows( $result);

    if ($num_rows > 0):
    $count = 1;
    While ($num_rows > 0):
    //$username = $username.$coun t;
    $result = mysql_query("SE LECT Username FROM users WHERE Username =
    '$name'");
    $num_rows = mysql_num_rows( $result);
    print($username .$count);
    $count = $count + 1;
    endwhile;

    //----
    else:
    print("ok");

    endif;


    ?>


  • Randell D.

    #2
    Re: Testing if a value exists.

    "Andy Levy" <andy_levy@hotm ail.com> wrote in message
    news:O__nb.1837 $bo.26@news-binary.blueyond er.co.uk...[color=blue]
    > Hi
    >
    > Im trying to create a function that tests if a 'Username' exists in a
    > particular table. If the value is found then the code generates[/color]
    'Username1'[color=blue]
    > as the username, and check if that username exists. If it does, it
    > continues onto generating 'Username2' and so on.
    >
    > <?
    > //testing with username andrew
    > $name = "andrew";
    >
    > mysql_pconnect( "server", "username", "pass");
    > mysql_select_db ("db");
    > $result = mysql_query("SE LECT Username FROM users WHERE Username =
    > '$name'");
    > $num_rows = mysql_num_rows( $result);
    >
    > if ($num_rows > 0):
    > $count = 1;
    > While ($num_rows > 0):
    > //$username = $username.$coun t;
    > $result = mysql_query("SE LECT Username FROM users WHERE Username =
    > '$name'");
    > $num_rows = mysql_num_rows( $result);
    > print($username .$count);
    > $count = $count + 1;
    > endwhile;
    >
    > //----
    > else:
    > print("ok");
    >
    > endif;
    > ?>[/color]

    Your above method is very performance ineffective - You have the potential
    of having alot of read/write to the disk when it is unnesscessary.. . I'm new
    to MySQL... and believe you could use "LIKE" in your query - I've known it
    exists and I just played around with it for the first time... but you could
    try something like

    $result=mysql_q uery("SELECT Username FROM users WHERE Username LIKE
    '$name%';

    I believe the percent sign acts as a wild card, so it should catch
    "username", "username1" , "username99 9" all in one go... Play around with it
    and look up the 'LIKE' condition for more info... It is bound to be far more
    environmentally friendly on your systems resources than your existing
    method... You could go one step further and perform an ORDER BY Username and
    LIMIT too... this would help you determine the next username that you want
    to use...

    Hope that helps...
    randelld


    Comment

    • Geoff Berrow

      #3
      Re: Testing if a value exists.

      I noticed that Message-ID:
      <O__nb.1837$bo. 26@news-binary.blueyond er.co.uk> from Andy Levy contained
      the following:
      [color=blue]
      >Im trying to create a function that tests if a 'Username' exists in a
      >particular table. If the value is found then the code generates 'Username1'
      >as the username, and check if that username exists. If it does, it
      >continues onto generating 'Username2' and so on.[/color]

      try this:

      $db = mysql_connect(" host", "user", "pass");
      mysql_select_db ("dbname",$d b);
      $name = "michael";
      $result = mysql_query("SE LECT Username FROM users WHERE
      Username='$name '",$db);
      $num_rows = mysql_num_rows( $result);
      if ($num_rows > 0):
      $count = 1;
      While ($num_rows > 0):
      $newname = $name.$count;
      $result = mysql_query("SE LECT Username FROM users WHERE
      Username='$newn ame'",$db);
      $num_rows = mysql_num_rows( $result);
      print($newname) ;
      $count = $count + 1;
      endwhile;

      --
      Geoff Berrow
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Andy Levy

        #4
        Re: Testing if a value exists.

        Thanks Geoff & Randell

        That is very useful. I am going to use your help. I have also realised
        that i do not need an iterating loop though. If i just add the (numRows +1)
        onto the end of the username i should never come across a duplicate
        username. If the SQL uses the 'Like' operator then it should pull up all
        rows.


        "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
        news:jtm1qvorqc 231p1bk8l5bepi4 u0j1s1snj@4ax.c om...[color=blue]
        > I noticed that Message-ID:
        > <O__nb.1837$bo. 26@news-binary.blueyond er.co.uk> from Andy Levy contained
        > the following:
        >[color=green]
        > >Im trying to create a function that tests if a 'Username' exists in a
        > >particular table. If the value is found then the code generates[/color][/color]
        'Username1'[color=blue][color=green]
        > >as the username, and check if that username exists. If it does, it
        > >continues onto generating 'Username2' and so on.[/color]
        >
        > try this:
        >
        > $db = mysql_connect(" host", "user", "pass");
        > mysql_select_db ("dbname",$d b);
        > $name = "michael";
        > $result = mysql_query("SE LECT Username FROM users WHERE
        > Username='$name '",$db);
        > $num_rows = mysql_num_rows( $result);
        > if ($num_rows > 0):
        > $count = 1;
        > While ($num_rows > 0):
        > $newname = $name.$count;
        > $result = mysql_query("SE LECT Username FROM users WHERE
        > Username='$newn ame'",$db);
        > $num_rows = mysql_num_rows( $result);
        > print($newname) ;
        > $count = $count + 1;
        > endwhile;
        >
        > --
        > Geoff Berrow
        > It's only Usenet, no one dies.
        > My opinions, not the committee's, mine.
        > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


        Comment

        Working...