Closed Thread
Results 1 to 4 of 4




  

Thread: PHP for newbies

      
  1. #1
    The Force is Strong! DarrenC is just really niceDarrenC is just really niceDarrenC is just really niceDarrenC is just really niceDarrenC is just really niceDarrenC is just really niceDarrenC is just really nice
    Join Date
    September 23, 2006
    Posts
    637
    Rep Power
    6
    Feedback Score
    0

    Exclamation PHP for newbies

    I'm trying to learn PHP and mySQL which is going to be a tough since I'm not from a programming background.

    Let's start this simple by asking if there is a process to follow to create a new field in a database and outputting the data from that field in a php page?

    I think it would be useful to start with this and then build up our knowledge once we've got used to the basics.

    So you programming wizards Help me please! :cool03:

  2. #2
    The Force is Strong! boxxa will become famous soon enoughboxxa will become famous soon enough boxxa's Avatar
    Join Date
    April 20, 2006
    Location
    NY
    Posts
    155
    Rep Power
    7
    Feedback Score
    0

    Default

    The best way I learned was to figure out some type of project you want to develop. Choose something simple like a user login system. You from there can begin to understand the basics of PHP and MySQL.


    Here is a tutorial.

    First part of it is the database. You need a brief understanding of a database and how its setup. Since you are not deploying heave performance driven applications, simple make a database called mylogin (if your host is shared on cpanel, the database name is sometimes something like yourusername_mylogin). Once that is created then create a table called users. In the users table, make 3 fields: id, username, and password. The ID can be a TINYINT(2), Primary Key, and auto_increment and the other two can be VARCHAR(32). Also, make sure you add a record into that table with a username and password.

    Now for the script. None of these are tested or written to standard so give me a break

    First, you need to have a login form. Create a page called login.html with this code.

    Code:
    <html>
    <body>
    <center><h2>Please login</h2></center>
    <form name="login" method="POST" action="login.php">
    Username: <input type="text" name="username"><br/>
    Password: <input type="password" name="password"><br/>
    <input type="submit" value="Login" name="do_login">
    </form>
    </body>
    </html>
    Basically that is a simple HTML form with names on all the fields so we can access them in the PHP hander script. Also, for security reasons, your form method should always be POST unless you want the variable posted to the URL. Sometimes later you can use GET to handle certain page aspects but for now, leave it at POST.

    Now, you need to handler the form some how. In the FORM tag, we have action="login.php". This means that the form will send it self to that page. Lets begin to code that page.

    Save this page now as login.php

    Code:
    <?
    // First lets check to see if the form was submitted. This prevents someone from going to this page directly.
    if(isset($_POST['do_login'])){
    
    // It is so were now in here
    // Were gonna change the variables from $_POST to local ones for ease of coding.
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    // We also need to setup our mysql connection
    
    $mysqluser = "username_username";
    $mysqlpass = "password";
    $mysqlhost = "localhost";
    $mysqldb = "mylogin"
    mysql_connect($mysqlhost, $mysqluser, $mysqlpass) or die(mysql_error());
    mysql_select_db($mysqldb) or die(mysql_error());
    
    //Now we can connect to the database, lets try to see if we can login.
    
    //First select all records in the table with the username and password that match the one entered.
    
    $query = mysql_query("SELECT * FROM users WHERE username='$username' && password='$password") or die(mysql_error());
    
    // If the username and password was correct then there would be 1 row from the table returned. If one was wrong then there would be 0.
    
    $rows = mysql_num_rows($query);
    if($rows == 0){
    echo "<center>Invalid username/password</center>";
    } else {
    echo "<center>You have entered a correct username and password.</center>";
    }
    
    }
    
    ?>
    Thats it! Now you can expand on that and carry that login through differnet pages using some more features (pretty simple). I hope that helps. Next work on adding and removing data from a database. I can write another tutorial for that one if you would like.
    Bryan
    Your stupidity makes me 1337
    My Blog | Web hosting!

  3. #3
    I'm New! cr0n1cal is on a distinguished road
    Join Date
    February 7, 2007
    Posts
    19
    Rep Power
    6
    Feedback Score
    0

    Default

    DarrenC, create one concrete situation or project , simple on the first phase and try step by step to resolve all problems. I spend one hour to realise connection between my MySql server and Dreamweaver. More people said me: don't try to discover the wheel ... but when you try to solve all small problems you will see that the project is solved.

    That is my idea about learning.
    Good Luck!
    Cr0n

  4. #4
    Grand Masters The Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud ofThe Webmaster has much to be proud of The Webmaster's Avatar
    Join Date
    December 10, 2006
    Posts
    1,709
    Rep Power
    7
    Feedback Score
    0

    Default

    Quote Originally Posted by DarrenC View Post
    I'm trying to learn PHP and mySQL which is going to be a tough since I'm not from a programming background.

    Let's start this simple by asking if there is a process to follow to create a new field in a database and outputting the data from that field in a php page?
    Darren.. The Best way to learn any programming language is to start a simple project...

    I see you asked how to create a new field in a database..

    Allow me to give you little intro of SQL based RDBMS (Relational DatBase Management Systems)

    There are three major parts of any SQL database
    1. The Database itself
    lets take an example, suppose we created a database named 'school_records'.

    2. Tables in the database
    Tables are different data tables that contain variety of Records..
    Like in the database 'school_records' we have two tables 'tbl_students' and 'tbl_teachers'..
    The student table will hold recods for Student, same way Techers table will hold records for techers..

    3. Records or Fields in the table
    Each record or field is a different data type in tables..
    Suppose in table 'tbl_students' we have following records:
    a) student_id
    b) student_name
    c) student_grade

    The student_id will be a long integer data type that will hold identification no. of students and will be unique for each student

    student_name will be a String data type

    student_grade will again be a integer..


    Now the outline of the database is ready and all you need is to create the database using php, lets do it step by step..

    Step 1
    First you need to create a database.. You can create a new database using cpanel's database options..
    Once you have created the database named 'school_records' you will need to add a user and password to access the database..that too you can create using cpanel..
    Suppose we have created a user: username with password: password for our databse...
    step one is done and now we will move to step two

    Step 2
    Once the databse is all set and done.. you need to create a connection between your site and the database..
    We will now use PHP for this purpose..

    <?php
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    echo "Connected to MySQL<br />";
    ?>

    Now let me explain the code:
    <?PHP tells that a PHP code is about begin
    function mysql_connect() does what name suggests, it connects to the database server.. as you can see there are three parameters in the function
    1. "localhost" - This is address of your DB server, usually you can leave it as it is, because most webhosts use the same server for both web server and DB server, but if your host has a distinct DB server then you should put the domain name or the IP of that server..

    2. "username" - it is the username that we added into database in the step 1

    3. "password" is the password we given to user in step 1

    If everything goes fine then the function will be executed and the page will display - Connected to MySQL
    but in case there is any error then die() will terminate the connection and will show the error..

    Now once we are connected to the DB server We need do to choose our working database i.e. 'school_records', We will do so with -

    <?PHP
    mysql_select_db("school_records") or die(mysql_error());
    echo "Connected to Database";
    ?>

    If everything is fine then we are connected to the databse, in case theres any error, it will e shown on the screen.

    Now step 2 is done, we are connected to the database server and successfully chosen our working DB, so it time to move to STEP three..

    that is to create tables in the database

    Step 3
    We rae going to create the table 'tbl_students'

    <?
    mysql_query("CREATE TABLE tbl_students(
    student_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(student_id),
    student_name VARCHAR(30),
    student_grade INT)") or die(mysql_error());

    echo "Table Created!";
    ?>

    This is a bit longer code, so let me explain,
    function mysql_query() sends the query to MySQL server,
    'CREATE TABLE' is pretty easy to understand as it creates table name 'tbl_student' along with its fields.

    'student_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(student_id),'
    - this first field to be created, INT states its Data type, its integer.

    NOT NULL tells that this field cant be left blank.

    Since it should be unique for each student AUTO_INCREMENT makes sure that, it will automatically increases the value of next record, so you dont need to mention it every time you create a new record.

    The Same way we created next two fields..

    now we have completed all three steps required to make a database and tables inside it..

    Hope I made some sense..

    Ohh yeah and for more details about MySQL and PHP dont forget to visit this site- http://www.tizag.com/mysqlTutorial/

    Tizag has an excellent resource for beginners..

    Good Luck

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. What newbies would love to hear about SEO?
    By Manish Pandey in forum SEO Forum
    Replies: 25
    Last Post: Dec 3rd, 2007, 6:05 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
WebTalkForums
WebTalkForums
Recent Forum Threads