Results 1 to 2 of 2

Thread: mySQL n00b

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2003
    Location
    Cambs, UK
    Posts
    55

    mySQL n00b

    Heya.... I've just recently started piddling around with PHP, and found I liked it (wehey )
    I've started on a side project of a simple login/register page, which connects to my mySQL db, checks the info, then lets users in or whatever... however, I have NO idea how to get PHP to talk to the db.... any starters?




    Thanks for yer help in advance

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    Well, since you're new to PHP, I'm guessing you don't know a whole lot about the language.. so, here's a pretty basic snippet that will show you what you can/want to do.. it might help you out a bit..

    PHP Code:
    <?
      //set your mysql connect variables
        $mysql['host'] = "localhost";    //host address
        $mysql['user'] = "user";         //username
        $mysql['pass'] = "pass";         //password
        $mysql['maindb'] = "db_name";    //database name
        $mysql['table'] = "table_name";  //table name
      //make an empty mysql query and mysql data array
        $mysql['query'] = array();
        $mysql['data'] = array();
      //connect to mysql host with user:pass combination
        //note: @ will stop errors from being displayed
        @mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']) or die("MySQL Error: Database connection error");
      //select the mysql database
        @mysql_select_db($mysql['maindb']) or die("MySQL Error: Database selection error");

      
      //make a mysql query to select information from the database
        $mysql['query'][0] = "SELECT field1, field2 FROM $mysql[table] WHERE field1='some_data'";
      //make a mysql query to insert information into the database
        $mysql['query'][1] = "INSERT INTO $mysql[table] VALUES('$some_data', '$some_data2', '$some_other_data')";
      //make a mysql query to update information in the database
        $mysql['query'][2] = "UPDATE $mysql[table] SET field1='some_other_data' WHERE field1='some_data'";

      //query the database for each mysql query which does not return data (INSERT, UPDATE)
        mysql_query($mysql['query'][1]);
        mysql_query($mysql['query'][2]);
      //query the database for the mysql query that does return data (SELECT)
        $query = mysql_query($mysql['query'][0]);
      //fetch the data that the query returned
        //use just a flat out mysql_fetch_array($query) to get one result
          $mysql['data'][0] = mysql_fetch_array($query);
          //print out the array
            echo "<xmp>";
            print_r($mysql['data'][0]);
            echo "</xmp>";
        //OR, use a while loop to fetch all data that fits the SELECT
          //store what the current record is
          $i = 0;
          while($mysql['data'][1] = mysql_fetch_array($query)){
            //print out the current returned results
              echo "<xmp>";
              print_r($mysql['data'][1]);
              echo "</xmp>";
            $i++; //increase the current record number
          }
    ?>
    I realize that just giving you that with my crude comments will probably not do a lot, but, you did ask for how to interact with a database, and that's how.. Anyway, I suggest looking up some MySQL tutorials, www.3dbuzz.com has some good PHP/MySQL VTMs.

    PS: With your login system, you're going to want to use MySQL SELECTs to check the information.. another code snippet.. this is pretty much all you would have to do, with a lot of customization, of course:

    PHP Code:
    <?
      //mysql connection stuff goes here
        $mysql['table'] = "users";
      if(!isset($_POST['user'], $_POST['pass'])){
        //form hasn't been submitted, show it
    ?>
    <form method="post">
      username: <input type="text" name="user">
      password: <input type="password" name="pass">
      <input type="submit" value="login">
    </form>
    <?
      }else{
        //form has been submitted, check login information

          //note: for ease of use, ONLY select the password
          //      if the password can't be located from the WHERE fields, the password is wrong
          //note2: if you want to return an error if the account even exists, you can add to this
          //note3: 'LIMIT 1' limits the number of records returned to 1

          //set up the query to match the login information to a database record
            $query = "SELECT pass FROM $mysql[table] WHERE user='$_POST[user]' AND pass='$_POST[pass]' LIMIT 1";
          //query the database
            $query = mysql_query($query);
          //fetch the query array
            $query = mysql_fetch_array($query);
          //note: if you are looping through the query, you must use different variable names
          //      for the mysql_query() and the mysql_fetch_array()
          if(!empty($query[0])){
            //query was not empty, login information is correct
            echo "you logged in successfully!";
          }else{
            //query was empty, login information was incorrect
            echo "there was an error with your login information!";
          }
      }
    ?>
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width