How to check if a record exists in a MySQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vivekgoyal
    New Member
    • Sep 2007
    • 6

    How to check if a record exists in a MySQL database

    hello

    i have some problem..
    actually i make a userscreen using php and problem is that i have check the username is already exists aur not...
    i used the query
    like this...
    [code=php]
    $con = mysql_connect(" localhost","roo t");
    if(!$con)
    {
    die('Could not connect:'.mysql _error());
    }
    mysql_select_db ("demo",$con );
    $obj1=mysql_que ry("select count(u_Usernam e) as test from user where u_Username='$_P OST[username]'");
    echo ($obj1);
    $sql="Insert into user(u_id, u_Fname, U_Lname, u_Email, u_Username, u_password, u_Cpassword) VALUES
    (NULL, '$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[username]', '$_POST[password]', '$_POST[repassword]');";
    $objRecordSet = mysql_query($sq l);
    $errorNo = mysql_errno();
    if($errorNo == 0)
    {
    ?>
    <script type="text/javascript">
    alert("Record Saved");
    </script>
    <?php
    }
    mysql_close($co n);
    [/code]

    this is my code...
    i want the no of records in the $obj1;;;
    but this coding return me the Resource id #3

    so anyone can solve my problem....
    actually i am totally new in php....
    so plz help...
    thanks
    Last edited by Atli; Sep 12 '07, 04:33 PM. Reason: Added [code] tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Please enclose any code within the appropriate code tags. See Posting Guidelines .

    Actually variable $obj1 is not the result of the query but the resource handle for the result. You must fetch your result data using that handle. The number of rows can be extracted in variable $no with
    [php]$no = mysql_num_rows( $obj1);[/php]
    Ronald

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      I've edited the thread title to make it a little bit more clear.

      Comment

      • Weisbartb
        New Member
        • Aug 2007
        • 36

        #4
        $obj1 is a mysql resource id. Please use mysql_fetch_ass oc or mysql_fetch_obj ect. Example below
        [php]

        $row = mysql_fetch_ass oc($obj1);
        [/php]

        However for what you need just run
        [php]
        if(mysql_num_ro ws($obj1) > 0){
        //name exists
        }else{
        //Name doesn't exist
        }
        [/php]

        Comment

        • vivekgoyal
          New Member
          • Sep 2007
          • 6

          #5
          thanks ronald.....

          to solve my problem....

          i solve this problem with ur help.

          today i have posted new problem please solve this one also....


          Originally posted by ronverdonk
          Please enclose any code within the appropriate code tags. See Posting Guidelines .

          Actually variable $obj1 is not the result of the query but the resource handle for the result. You must fetch your result data using that handle. The number of rows can be extracted in variable $no with
          [php]$no = mysql_num_rows( $obj1);[/php]
          Ronald

          Comment

          • vivekgoyal
            New Member
            • Sep 2007
            • 6

            #6
            thanks Weisbartb


            to solve my problem...
            thanks once again...

            Originally posted by Weisbartb
            $obj1 is a mysql resource id. Please use mysql_fetch_ass oc or mysql_fetch_obj ect. Example below
            [php]

            $row = mysql_fetch_ass oc($obj1);
            [/php]

            However for what you need just run
            [php]
            if(mysql_num_ro ws($obj1) > 0){
            //name exists
            }else{
            //Name doesn't exist
            }
            [/php]

            Comment

            Working...