How to connect to MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AP

    How to connect to MySQL

    I have not seen any good examples on how to connect to a mySQL db. My
    website uses phpMyAdmin to administer the SQL databases. What confuses
    me is where are the databases that I create located? For example, if my
    website is www.mywebsite.com and I have installed myphpadmin in the
    folder tkd, now using this I have created two databases, one named
    'acct' with a single table named 'persons', and another database named
    'terms' with a single table named accounts. How would I specify in php
    that I wanted to connect to the acct database and pull from the persons
    table?

    Thanks

  • Botan Guner

    #2
    Re: How to connect to MySQL

    Phpmyadmin is a web base mysql databse administration tool, it doesn't
    matter where u install it under your webroot. It helps you to
    adminstrate mysql db using a web browser insted of using command line
    mysql> tool.

    When you create a db using phpmyadmin or in any other way, mysql stores
    the data files in a directory (Data Directory) which is specified in
    my.ini file.

    After adding a user with appropriate privileges you can connect to
    mysql localy or remotely.

    If you are going to use only one table you can select that db after
    connecting to mysql like,

    //copy paste from php manual
    $link = mysql_connect(' localhost', 'mysql_user', 'mysql_password ');
    if (!$link) {
    die('Not connected : ' . mysql_error());
    }

    // make foo the current db
    $db_selected = mysql_select_db ('foo', $link);
    if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
    }

    you can use mysql_db_query( ) function to send a query to terms database
    while acct is selected.

    You may want to look at php manuel...

    Comment

    • maildipin@gmail.com

      #3
      Re: How to connect to MySQL

      yes you can use mysql_db_query function as,
      ............
      mysql_connect($ hostname, $user, $passwd);

      $result = mysql_db_query( $db1, $query1);
      $row1 = mysql_fetch_obj ect($result);

      $result = mysql_db_query( $db2, $query2);
      $row2 = mysql_fetch_obj ect($result);
      .............

      Comment

      • gd

        #4
        Re: How to connect to MySQL

        mysql_connect($ hostname, $user, $passwd);

        $result = mysql_db_query( $db1, $query1);
        $row1 = mysql_fetch_obj ect($result);


        $result = mysql_db_query( $db2, $query2);
        $row2 = mysql_fetch_obj ect($result);

        Comment

        • Lennart Björk

          #5
          Re: How to connect to MySQL

          Botan Guner,

          I have a similar problem and I use exactly your code
          [color=blue]
          >[/color]
          //copy paste from php manual
          $link = mysql_connect(' localhost', 'mysql_user', 'mysql_password ');
          if (!$link) {
          die('Not connected : ' . mysql_error());
          }
          <

          and I get
          Parse error: parse error, unexpected T_IF in
          my program (or your program or the manual's program)
          on line
          'if (!$link) {'

          Something must be wrong in my setup. Can you give me a hint?


          Lennart

          Comment

          • Jerry Stuckle

            #6
            Re: How to connect to MySQL

            Lennart Björk wrote:[color=blue]
            > Botan Guner,
            >
            > I have a similar problem and I use exactly your code
            >[color=green]
            > >[/color]
            > //copy paste from php manual
            > $link = mysql_connect(' localhost', 'mysql_user', 'mysql_password ');
            > if (!$link) {
            > die('Not connected : ' . mysql_error());
            > }
            > <
            >
            > and I get
            > Parse error: parse error, unexpected T_IF in
            > my program (or your program or the manual's program)
            > on line
            > 'if (!$link) {'
            >
            > Something must be wrong in my setup. Can you give me a hint?
            >
            >
            > Lennart[/color]

            Lennart,

            I don't see an error there. I suspect it's someplace earlier in your
            code. Probably mismatched single quotes.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Lennart Björk

              #7
              Re: How to connect to MySQL

              Jerry,
              [color=blue]
              > I suspect it's someplace earlier in your code[/color]

              You are right and the error was mine. I forgot a semicolon.

              Lennart

              Comment

              Working...