1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Please Help Me My SQL

Discussion in 'Web Design & Development' started by dudeking, Jul 11, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    Right I need to use mysql.

    If some one would please post a PHP script that connected to a sql database, stored a value in the database then echo the value back out of the database.

    If I saw a script that did that I would be able to start using my sql with my site. I just have no clue where to start at the moment.


    Thank you soooo much any one who does this for me :D :D (y) (y)
     
  2. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Put together from http://www.w3schools.com/php/php_mysql_intro.asp
    You can learn almost all there is to know there. :)
    PHP:
    <?php
    $con 
    mysql_connect("localhost","dbuser","dbpass");
    if (!
    $con)
        die(
    'Could not connect: ' mysql_error());

    mysql_query("CREATE DATABASE my_db",$con);
    mysql_select_db("my_db"$con);
    $sql "CREATE TABLE person 
    (
    FirstName varchar(15),
    LastName varchar(15),
    Age int
    )"
    ;
    mysql_query($sql,$con);
    mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
    mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");
    $result mysql_query("SELECT * FROM person");
    echo
    "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    </tr>"
    ;

    while(
    $row mysql_fetch_array($result))
    {
        echo 
    "<tr>";
        echo 
    "<td>" $row['FirstName'] . "</td>";
        echo 
    "<td>" $row['LastName'] . "</td>";
        echo 
    "</tr>";
    }
    echo 
    "</table>";
    mysql_close($con);
    ?>
    In this code I create a db, a table, and two rows.

    You should only have to do that once.
     
  3. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    thank you sooo much :)

    I owe you one mate!!
     
  4. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    so do i have to have some code to check if the table and rows are already there?
     
  5. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Add IF NOT EXISTS

    Example:

    PHP:
    $sql "CREATE TABLE IF NOT EXISTS person 
    (
    FirstName varchar(15),
    LastName varchar(15),
    Age int
    )"
    ;
     
  6. dudeking

    dudeking Thread Starter

    Joined:
    Feb 7, 2007
    Messages:
    483
    Okay, so right, I want to create user account for my site.

    Do I have a separate row for each member then. So a registration form people enter info and its exported to a row on the members table. Some one comes to the site, it sees there is no cookie for the site, so it says login instead of saying welcome 'Your Name'. Then people click login enter there username and password, now this is the bit im stuck on. Do i need to seach the database for that username then see if the password matches up? Or is there some other more secure way to do this?
     
  7. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    You will do something like mysql_query("SELECT * FROM users WHERE username = $_POST['username'] AND password = $_POST['password']")

    Don't use that code though, it might be incorrect and will definitely be a security risk.

    There are hundreds of tutorials on the net about this issue. Google is your friend! :)
     
  8. JRWalker

    JRWalker

    Joined:
    May 21, 2007
    Messages:
    43
    Yes, you will need a row for each member. In the database you will have a username and password field. The password field should be varchar, 32 characters long. To be more secure, you will want to encrypt what the user enters into the password text box using md5 encryption. This encrypted password will be stored as a 32 character long hash. When you check to see if the username and password match up, you compare the md5 hash from the password field with the md5 hash in the database, that way no plain text password will be stored. It might also be wise to use session variables that are persistent across pages, start your pages with <?php $session_start; ?>. You can store variables in the session with the following:
    <?php $username = $_SESSION['username']; ?> With this method you can access the username from different pages.
     
  9. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    That would be session_start(), and $_SESSION['username'] = $username.
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/594412

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice