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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
ddesai
  • 499
  • 1
  • 6
  • 17
  • 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 ? – adeneo Apr 24 '15 at 12:55
  • possible duplicate of [PHP - MYSQL - test database server](http://stackoverflow.com/questions/13870362/php-mysql-test-database-server) – Saty Apr 24 '15 at 12:58
  • possible duplicate of [Warning: mysql\_connect(): \[2002\] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in](http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi) – Jonathan Apr 24 '15 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. – ddesai Apr 24 '15 at 13:13

1 Answers1

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 ?

Prakash
  • 171
  • 7
  • 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 Apr 24 '15 at 13:26
  • @ddesai it looks like your username or password is incorrect. Have you checked these? Have you setup the MySQL user on your MySQL instance? – developer__c Apr 24 '15 at 13:32
  • @ddesai, Yes your username or password is incorrect. – Prakash Apr 24 '15 at 13:40
  • 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. – ddesai Apr 24 '15 at 13:45