Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    13
    Member #
    17663
    hey guys, i have a quick question... im trying to get into databasing with php and wrote this piece of code

    PHP Code:
    <?php
    //set variables
    $user $_POST[UserName];

    $pass $_POST[Password];

    $flag 0;

    //checks if variables are set and passed correctly
    echo "$user " $pass;

    // Begins mysql connection
    $con mysql_connect("localhost","root","polerty");
    if (!
    $con) { //if could not connect
        
    die ("Connection Error: " mysql_error());//kill script and display error   
    } else { //if connection succesful, display connection succesful
        
    echo "<br>connection successful";    
    }


    mysql_select_db("users",$con); // Selects the Database we're working with and connects using var con

    $results mysql_query("SELECT Usernames FROM members"); //sets var results to the mysql query command
                                                                                                                              
    /* the while loop continues while it is set true (while there are rows to fetch) the rows get set to 
    an array called var users and can be called by the name of array and the name of the column/index 
    ie. (array[columnName])  */
    while($users mysql_fetch_array($results))
    {
        
    //if the var user (the users input) equals the fetched array (the stored information)
        
    if ($user == $users['Usernames'])
        {   
         
    $flag=1;//then set the flag to one   
        
    }
    }
        
    // if the flag is set to 1, then display error message along with the form
    if ($flag == 1){
        echo 
    "Username already taken <br>";
        include(
    "index.html");
    //if its not set to 1, then store the users input into the database    
    } else {
        
    //if theres an error while trying to add the username and password to the database, then display it
        
    if(!mysql_query("INSERT INTO members (Usernames, Passwords) VALUES ('$user','$pass')"))
        echo 
    mysql_error();
        
    }

    mysql_close($con); // close mysql connection
    ?>
    i know its long im sry.. im new to this and i don't know which parts you guys need to see but i was looking around and stumbled on this piece of code

    PHP Code:
    $result=mysql_query("select * from users  
      where username='" 
    $_SESSION['username'] . "' and password='" $_SESSION['password'] . "'"); 

    // retrieve number of rows resulted 
    $num=mysql_num_rows($result);  

    // print login form and exit if failed. 
    if($num 1){ 
      echo 
    "You are not authenticated.  Please login.<br><br> 
       
      <form method=POST action=index.php> 
      username: <input type=text name=\"username\"> 
      password: <input type=password name=\"password\"> 
      <input type=submit> 
      </form>"

       
      exit; 

    my question is, which script would be more efficient? the one that checks the tables to see if its in the listing or the one that uses the mysql check. any pros and cons?

  2.  

  3. #2
    Senior Member imagn's Avatar
    Join Date
    Jul 2007
    Location
    Los Angeles
    Posts
    156
    Member #
    15594
    I'm confused... are you trying to validate against records with duplicate usernames or authenticate a login...


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:44 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com