Search code examples
phpmysqlphpmyadminxamppmamp

Connecting PHP through xAMPP/MAMP to mySQL Database


I can connect to phpMyAdmin database through both xAMPP and MAMP from my php code. I can successfully insert data into my database but it sends it to the database in PHPMyAdmin which is completely different from the database that I want to send it to which is in MySQL. I want it to insert it into the table in MySQL. Is there a way I can fix this?


Solution

  • Both XAMPP and MAMP assume that they will be providing everything for your installation. This means that XAMPP is running it's own web server and database server and MAMP is running it's own web server and database server.

    Each database server (the one under XAMPP and the one under MAMP) has it's own databases, tables, etc and making changes on one does not carry over to the other.

    For them to both see the same databases and tables, you need to either:

    a) Configure both XAMPP and MAMP to connect to the same database server (whether it be the one for XAMPP or MAMP is your choice).

    b) Do some complicated replication between to two servers (yes, they may be running on the same physical machine, but each runs it's own database server process)

    c) Setup MySQL separate from XAMPP and MAMP and direct the two to use the separate MySQL database.

    As it's been noted phpMyAdmin is NOT a database. It is a web application that connects to a MySQL database (much like your website) to provide an easy to use interface for managing the underlying SQL database.

    EDIT:

    If you are trying to connect to a database programmatically via PHP, you have two options:

    mysqli

    $mysqli = new mysqli("example.com", "user", "password", "database");
    

    PDO

    $pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
    

    There is an article on PHP.net that compares features of the different APIs that you should consult: http://php.net/manual/en/mysqlinfo.api.choosing.php

    From there, you will need to consult the PHP documentation as to how to read from and write to the database.