Connecting to Mysql in PHP using DSN

Discussion in 'PHP' started by surfnearnzone, Dec 14, 2006.

  1. #1
    Can anyone tell me if there is any way to connect to a mysql database in PHP using DSN?? I have a windows hosting account with features of PHP and MYSQL database. Please help me.
     
    surfnearnzone, Dec 14, 2006 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    mysql_connect("hostname", "username", "password");
    mysql_select_db("database");
     
    krakjoe, Dec 14, 2006 IP
  3. ico

    ico Peon

    Messages:
    126
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    //localhost settings replace the username and password of database server add
    $c_username = "root";
    $c_password = "";
    $c_database = "db_name";

    // Connect.
    $connection = mysql_connect('localhost', $c_username, $c_password);

    mysql_select_db($c_database);
     
    ico, Dec 16, 2006 IP
  4. surfnearnzone

    surfnearnzone Well-Known Member

    Messages:
    1,492
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    165
    #4
    Thanks. Let me try this.

    Actually i work in asp. where the db connection using dsn works like this

    dim cn // declare variable
    set cn = server.createobject("adodb.connection") //Declare adodb connection
    cn.open "dsn=pebdb;"  // Declare databse connection where pebdb is databse dsn
    Code (markup):
    this info is saved in a file cn.asp which is used wherever connection is required.

    Should i do the same with the following code.

    K.i.P.


     
    surfnearnzone, Dec 16, 2006 IP
  5. ico

    ico Peon

    Messages:
    126
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    asp and mysql are not really that compatible to use i dont know if that will work, lemme try to find out...
     
    ico, Dec 18, 2006 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    using dsn isn't necessary, php has the ability to communicate with MySQL processes using php_mysql.dll http://php.net/mysql_connect it's pretty straight forward, if you can asp then you can php with some patience......
     
    krakjoe, Dec 18, 2006 IP
  7. surfnearnzone

    surfnearnzone Well-Known Member

    Messages:
    1,492
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    165
    #7
    No No don't get me wrong. I am not trying to establish dsn connection in asp. I just gave an example to better understand the process in PHP.

    I want to do mysql connection in PHP using DSN. In asp using DSN helps in making easy connection. Also the code is easier to remember. Thats why i wanted to know if there is a similar process in PHP too.

    Thanks


    K.i.P


     
    surfnearnzone, Dec 19, 2006 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    Oh okay, then it'll be the code either me or ico posted first time around :)
     
    krakjoe, Dec 19, 2006 IP
  9. ico

    ico Peon

    Messages:
    126
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    oh i get it... :)
     
    ico, Dec 19, 2006 IP