5

I'm learning PHP MYSQL and trying to create a database.

I'm following this tutorial http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

So far I'm testing to see if the server has access to MySQL. When I use the following code.

?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();

?

I get the following error:

Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/user/Sites/promos/index.php on line 8

Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2002): No such file or directory in /Users/user/Sites/promos/index.php on line 8

Warning: mysqli::autocommit() [mysqli.autocommit]: Couldn't fetch mysqli in /Users/user/Sites/promos/index.php on line 9

Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in /Users/user/Sites/promos/index.php on line 20

Fatal error: Call to a member function execute() on a non-object in /Users/user/Sites/promos/index.php on line 21

Did I perhaps forget to enable something?

It might explain why I cannot connect via my IP address to sequel pro and only to 127.0.0.1?

6
  • 1
    Do you have mysqli installed? And is it started? Commented Dec 13, 2012 at 23:29
  • I'd start with a much more basic example. Commented Dec 13, 2012 at 23:32
  • Locally hosted? Linux can be a challenge setting up a server. Commented Dec 13, 2012 at 23:33
  • I have mysql installed, I have connected to sequel pro only via my local address though Commented Dec 13, 2012 at 23:34
  • I know that for mysql, it can be related to the line mysql.default_socket in the php.ini file. You should check a similar entry in this file Commented Dec 13, 2012 at 23:35

3 Answers 3

17

Apache servers use '127.0.0.1' instead of 'localhost'

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

3 Comments

This works, but could you possibly explain why in a little more detail please? I'm just trying to get my head around all of this.
As explained by @mingos this is a common issue. It appears 'localhost' doesn't point to the correct socket within the Apache server. This is rectified by using 127.0.0.1
It is nothing to do with apache. Mysql use 'localhost' via 'UNIX socket', and '127.0.0.1' via 'TCP/IP'. So specify the right path of 'mysql.sock' if you use 'localhost'. dev.mysql.com/doc/refman/5.6/en/connecting.html#id471316
10

There are two settings you have to check for this to work (assuming you have MySQL server installed of course):

Check the value of mysql.default_socket in your PHP configuration.

Check the value of socket in your MySQL configuration file under the [mysqld] heading.

Those values have to be identical; if they're not, change one to match the other and restart respective service.

1 Comment

mysqli use mysqli.default_socket instead. and pdo_mysql.default_socket for pdo. We could check this through php -i | grep -F .default_socket
2

This seems to be a common issue, as googling for it yields quite a few results. I experienced this on my two linux boxes as well (never under Windows though) and at some point I resolved to just use 127.0.0.1 on all dev servers. Basically, localhost makes the connection to the MySQL server use a socket, but your configuration doesn't point to the socket file correctly.

Here are a couple of resources that you might find useful:

http://westsworld.dk/blog/2011/03/problems-connecting-to-unixvarmysqlmysql-sock/ - basically what @Jack suggests, but more in depth

Can't connect to MySQL on Mac -- missing mysql.sock file - here's how to locate your socket file (for some reason, my php.ini had the path wrong, and I assume your case could be similar).

I hope this helps.

Comments

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.