0

As a part of assignment I am trying to connect my Apache server to MySql database.

I have verified that my Apache is working with following code:

<?php

class RedeemAPI {
    // Main method to redeem a code
    function redeem() {
        echo "Hello, PHP!";
    }
}

// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();

?>

But when I replace this code to read from my database it gives me following error:

<?php

    class RedeemAPI {
        private $db;

        // Constructor - open DB connection
        function __construct() {
            $this->db = new mysqli('localhost', 'username', 'password', 'promos');
            $this->db->autocommit(FALSE);
        }

        // Destructor - close DB connection
        function __destruct() {
            $this->db->close();
        }

        // Main method to redeem a code
        function redeem() {
            // Print all codes in database
            $stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
            $stmt->execute();
            $stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
            while ($stmt->fetch()) {
                echo "$code has $uses_remaining uses remaining!";
            }
            $stmt->close();
        }
    }

    // This is the first thing that gets called when this page is loaded
    // Creates a new instance of the RedeemAPI class and calls the redeem method
    $api = new RedeemAPI;
    $api->redeem();

    ?>

Error: Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/promos/index.php on line 8

Warning: mysqli::autocommit(): Couldn't fetch mysqli in /Library/WebServer/Documents/promos/index.php on line 9

Warning: mysqli::prepare(): Couldn't fetch mysqli in /Library/WebServer/Documents/promos/index.php on line 20

Fatal error: Call to a member function execute() on a non-object in /Library/WebServer/Documents/promos/index.php on line 21

Please let me know what is causing this and how can i resolve this.

4
  • 2
    Not sure why or how you'd connect Apache to MySQL, you're sure you don't really want to connect PHP to MySQL instead ? Commented Apr 24, 2015 at 12:55
  • possible duplicate of PHP - MYSQL - test database server Commented Apr 24, 2015 at 12:58
  • possible duplicate of Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in Commented Apr 24, 2015 at 12:59
  • Yes I want to Connect PHP to mySQL and use Apache to see my database data via local host request. This is all new to me and am trying to learn how to create a web service on database so that I can further leverage it. Thanks for your reply. Commented Apr 24, 2015 at 13:13

1 Answer 1

1

To connect php to mysql, you need to place following code to check whether the connection is done -

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Let me know what error coming now ?

Sign up to request clarification or add additional context in comments.

4 Comments

Here is the error: Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in /Library/WebServer/Documents/promos/index.php on line 7 Connection failed: Access denied for user 'username'@'localhost' (using password: YES)
@ddesai it looks like your username or password is incorrect. Have you checked these? Have you setup the MySQL user on your MySQL instance?
@ddesai, Yes your username or password is incorrect.
So i restarted my sql server from system preferences and tried to login to my server from command line and it gave me following error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2). I think my SQL server is messed up. I think I should try to reinstall it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.