[tutorial]connecting to mysql using php

Discussion in 'PHP' started by SecureWebDev, Feb 21, 2008.

  1. #1
    Hey there,
    today we will be connecting to a mysql database using PHP functions.
    Before we begin it would be preferable that you know what the following info:
    1) The mysql host (usually localhost but varies when using a free host)
    2) The mysql username (the username that you will use to access mysql)
    3) The mysql password (the password that goes with the username)
    4) The mysql database (the database in mysql that contains all the info)

    Now lets begin,
    first open up notepad,phpdesigner,ect... and write the following:

    <?php
    //the usual format for php code looks like so, begins with <?php and ends with ?>
    ?>

    next well declare variables in order to use them for connection to the database

    <?php

    $host = "localhost"; //Change this to your host or leave it alone if your not sure
    $username = "swd"; //Change this to your username
    $password = "swd_real_hard_password"; //Change this to your password
    $db = "my_db"; //Change this to your database

    ?>

    Now after we declared our variables and gave them the correct value we will connect to the database using the mysql_connect function in php

    <?php

    $host = "localhost"; //Change this to your host or leave it alone if your not sure
    $username = "swd"; //Change this to your username
    $password = "swd_real_hard_password"; //Change this to your password
    $db = "my_db"; //Change this to your database

    $connect = mysql_connect($host,$user,$password); //connects to the mysql database

    ?>

    Now we might have connected to the database or not what we want to do now is check and see if we successfully connected if not we will print an error message


    <?php

    $host = "localhost"; //Change this to your host or leave it alone if your not sure
    $username = "swd"; //Change this to your username
    $password = "swd_real_hard_password"; //Change this to your password
    $db = "my_db"; //Change this to your database

    $connect = mysql_connect($host,$user,$password); //connects to the mysql database
    if(!$connect){
    //^ the above says if the connection is false ($connect is the connection and the ! mark means false)
    die("Error connecting to the mysql host"); //ends the script and echos an error
    }

    ?>

    now we will see if we connected correctly, if not go back and change your variables until you get it right
    now we need to select the database if the connection successfully went through so we will use the else condition

    <?php

    $host = "localhost"; //Change this to your host or leave it alone if your not sure
    $username = "swd"; //Change this to your username
    $password = "swd_real_hard_password"; //Change this to your password
    $db = "my_db"; //Change this to your database

    $connect = mysql_connect($host,$user,$password); //connects to the mysql database
    if(!$connect){
    //^ the above says if the connection is false ($connect is the connection and the ! mark means false)
    die("Error connecting to the mysql host"); //ends the script and echos an error
    }else{
    $select = mysql_select_db($db);
    }
    ?>

    now it might select the detabase or might not so well create another condition like the above

    <?php

    $host = "localhost"; //Change this to your host or leave it alone if your not sure
    $username = "swd"; //Change this to your username
    $password = "swd_real_hard_password"; //Change this to your password
    $db = "my_db"; //Change this to your database

    $connect = mysql_connect($host,$user,$password); //connects to the mysql database
    if(!$connect){
    //^ the above says if the connection is false ($connect is the connection and the ! mark means false)
    die("Error connecting to the mysql host"); //ends the script and echos an error
    }else{
    $select = mysql_select_db($db);
    if(!$select){
    die("Error selecting database.");
    }
    }
    ?>

    Now if everything goes as plans you will see no errors in your script
    now if you would like to use this script just include it into your files

    hope you learn something =)
     
    SecureWebDev, Feb 21, 2008 IP
    zangief likes this.
  2. able

    able Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not use constants for the database credentials?
     
    able, Feb 21, 2008 IP
  3. SecureWebDev

    SecureWebDev Active Member

    Messages:
    677
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #3
    that can also be used. Im thinking of using an encryption for further security if the website is compromised...
     
    SecureWebDev, Feb 21, 2008 IP