SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Understanding how PHP finds MySQL?!

    Hi all! Okay, here is probably the most vague, and dumbest, question of the week!!

    I am in the process of learning PHP, and knew it quite well back this summer. Unfortunately "LIFE" has come up since July, and all of my new-found knowledge with PHP and MySQL seems lost in the recesses of my mind!!

    On to my confusion...

    How does a PHP script and/or application know where to find MySQL and a particular MySQL database??

    I am desperately trying to finish my Seminar Registration project and have most of my web pages done, and now just need to write the backend code to get this thing up and running.

    There is a really great "User Registration Case Study" in my book "Visual QuickPro Guide: PHP 6 and MySQL 5". So all I should have to do is modify the author's scripts.

    I downloaded the author's files, but then I started wondering..

    1.) Where is MySQL on my MacMini?
    (I have a MacMini with OS X and am using MAMP.)

    2.) If I run the author's Database Scripts, how will MySQL see them?

    3.) Where will MySQL create the user database?

    4.) How will my PHP scripts know where the User MySQL database is?


    So, in summary, I'm just trying to get my head around how PHP and MySQL talk to each other and how to properly set things up.

    (I so totally understood this back in July! UGH!)

    I think a lot of my confusion comes from the fact that I am using NetBeans, and while I understand how to create PHP files and folders in NetBeans, I don't have the foggiest clue HOW or WHERE I instantiate MySQL databases and then point them to my PHP files in my NetBeans directory?!


    I am certain that the answers to my questions is super easy!!

    Hopefully someone can give me a general overview without their arms falling off!

    Thanks,


    Amy

  2. #2
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MySQL runs as a server, just use the php functions to connect to localhost.

    http://nz.php.net/pdo
    http://nz2.php.net/mysqli

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how MySQL uses memory and the file system for databases and tables is not of interest to PHP. So PHP doesn't care what the path to the MySQL files on the FS are. The MySQL libraries are installed or compiled into PHP and they give it the ability to communicate with a MySQL service (often on the same server, but it can be over a network).

    Using the mysql or mysqli libraries PHP 'connects' to the MySQL server and sends SQL queries to interact, like creating tables, selected data etc.
    The MySQL DB can also be administered with GUI tools installed on the server. PHP doesn't usually create the DB. You might do that with a GUI tool, or with the command line.

    Here's an example of PHP connecting to the MySQL service on the same machine, selecting an existing DB, and getting some data from it.

    PHP Code:
    $db = new mysqli('localhost''some_mysql_username''my_password''name_of_db');

    $result $db->query("SELECT sku, product_name, price FROM `products` WHERE price BETWEEN 50 AND 149");

    while(
    $row $result->fetch_assoc()) {
       
    printf('<p>%s (%s) costs $%d</p>'$row['product_name'], $row['sku'], $row['price']);


  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP knows the number of the port to which Mysql responds to by default.

    By (ab)using mysql's settings and altering that port number you'll likely break the connection (though I have never tried it).

    This may not be true, it is just how the 'internal map' inside my head understands it, I cough it up here in the hope that if I am wrong some kind soul will put us both on the path to enlightenment.

  6. #6
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I looked on the Start-up Page for MAMP and it gives you the Database Settings that should be used to connect to a MySQL database.

    Example:

    $link = mysql_connect('localhost', 'root', 'root');

    So I guess that is my answer.

    I'm still not sure where the actual MySQL files are located on my computer, but I also figured out that I can "see" the MySQL database using something like phpMyAdmin.

    Thanks,


    Amy

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    look for a folder on your computer called /data/

    if you have a database called test that'll become a folder, and inside that for each table there will be 3 files or so called

    .MYD, .MYI and .frm.

    Mysql server is a daemon which runs continuously waiting for requests to the port it listens to, 3306 IIRC.

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,019
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    One thing to note, when playing around with a MySQL server, never touch the database called "mysql" in any way! If you play around with that database you run a very, very, very high risk of breaking and/or corrupting the MySQL server!
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A lot of people (if not most) use pipes to connect to MySQL (most people do so unknowingly), not TCP/IP (using port 3306). If you use "localhost" for the server name, PHP will use Unix sockets/named pipes rather than TCP/IP. (Use "127.0.0.1" to force TCP/IP.)

    If you are on Linux or Mac OS X, you can find the local socket "file" that MySQL uses at /tmp/mysql.sock. That file is created when MySQL is started up.

    http://en.wikipedia.org/wiki/Unix_domain_socket
    http://en.wikipedia.org/wiki/Named_p...pes_in_Windows

  10. #10
    SitePoint Addict amy.damnit's Avatar
    Join Date
    Sep 2009
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    A lot of people (if not most) use pipes to connect to MySQL (most people do so unknowingly), not TCP/IP (using port 3306). If you use "localhost" for the server name, PHP will use Unix sockets/named pipes rather than TCP/IP. (Use "127.0.0.1" to force TCP/IP.)

    If you are on Linux or Mac OS X, you can find the local socket "file" that MySQL uses at /tmp/mysql.sock. That file is created when MySQL is started up.

    http://en.wikipedia.org/wiki/Unix_domain_socket
    http://en.wikipedia.org/wiki/Named_p...pes_in_Windows
    Hmmm, very interesting!


    Amy

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Thanks again sk89q

    Well I never. See? By joining in and piping up and sharing my ignorance we all learn something.

    Does this make me bright, brave or just daft?


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
  •