0

I am receiving the following error when loading my index page:

Fatal error: Call to a member function prepare() on a non-object in /Applications/MAMP/htdocs/parse/helloworld/helloworld/libraries/Database.php on line 32

I will place all of the relevant code below, sorry for the length but it should be fairly straight forward to read. The short version is I am practicing PDO and PHP classes so I am remaking an existing project I had on a different machine. (which is why a there is a lot of calls in the index file which acts more like a controller).

I am pulling my hair out here because it works on my other machine and from what i can tell the two projects are identical... I am just getting this error. I had this in my previous project, but that was because I had misspelled the database in the config file... I am 100% positive I did not do that again but I don't know what else I missed -- clearly the PDO class is not being made if I var_dump it returns null... Any and all help would be greatly appreciated (especially if it is in regards to my PDO class style I am just following a blog which seemed to make sense when it worked).

EDIT: After some debugging it was clear that the try block in the database class was failing because a connection could not be established. This was clear after running $this->error = $e->getMessage() which returned:

string(119) "SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)"

As the accepted answer below states the error indicates localhost trying to connect via a unix socket and mysqld not being able to able to accept unix socket connections.

So the original error leads to the ultimate question of: how do you connect to a unix socket when mysqld is not accepting unix socket connections and/or why does mysqld not accept unix socket connections?

End EDIT.

This is my (relevant) PDO class:

<?php
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;

private $dbh;
private $error;
private $stmt;

public function __construct() {
    // Set DSN
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
    // Set options
    $options = array (
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
    );
    // Create a new PDO instanace
    try {
        $this->dbh = new PDO ($dsn, $this->user, $this->pass, $options);
    }       // Catch any errors
    catch ( PDOException $e ) {
        var_dump($dsn);
        $this->error = $e->getMessage();
    }
}


public function query($query) {
    $this->stmt = $this->dbh->prepare($query);
}

This is the index file that gets called

<?php
require('core/init.php');

//Create objects...
$type = new Type;
$post = new Post;
$group = new Group;
$follower = new Follower;

//Create template object
$template = new Template('templates/front.php');

$template->types = $type->getAllTypes();
$template->groups = $group->getAllGroups();
$template->posts = $post->getAllPosts();
$template->replies = $post->getReplies();
$template->followers = $follower->getAllFollowers();

echo $template;

and these are the other relevant files: config --

<?php

//DB Params
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "root");
define("DB_NAME", "dobbletwo");
define("SITE_TITLE", "Welcome To Dooble!");

init --

//include configuration
require_once('config/config.php');

//helper functions
require_once('helpers/photo_helper.php');
require_once('helpers/system_helper.php');


//Autoload Classes
function __autoload($class_name){
    require_once('libraries/'.$class_name . '.php');
}

for the sake of brevity (which may be far gone at this point I will only show one of the classes i load to prove that I am constructing the db(type, post, group, follower, template...)

<?php

class Type {
//Initialize DB variable
public $db;

/*
*   Constructor
*/
public function __construct(){
    $this->db = new Database;
}
Mazzone
  • 401
  • 1
  • 6
  • 21
  • 1
    Is there anything in `$this->error` when this happens? – Barmar Apr 09 '15 at 02:41
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Sheepy Apr 09 '15 at 02:45
  • @Barmar it just spits out NULL from the catch block – Mazzone Apr 09 '15 at 02:55
  • @Sheepy thanks for the reference reading now to see if it is, regardless I would still appreciate any help, still very new to this! – Mazzone Apr 09 '15 at 02:56
  • @Mazzone That doesn't make sense. If it went into the catch block, `$e->getMessage()` should contain something. – Barmar Apr 09 '15 at 02:58
  • @Sheepy So from what I can make of the reference you gave there are similarities but it doesn't resolve my problem. It is similar because they both have the same error which clearly is because the $this->bdh does not get set... So really my question is more why the try block does not work? Why does new PDO fail? – Mazzone Apr 09 '15 at 03:00
  • @Barmar your totally right i think I forgot to save this is what is returned: string(119) "SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)" – Mazzone Apr 09 '15 at 03:03
  • @Barmar which i don't know why its saying that because i can connect through the terminal and through sequel pro – Mazzone Apr 09 '15 at 03:04
  • While stackoverflow is a programming solution site, it is not exactly designed for specific, localised debug questions which varies case by case. I'm pointing you to that question because others who asked the exact same error were pointed to there, which will hopefully guide you to better debug. – Sheepy Apr 09 '15 at 03:05
  • That error means the database isn't running on `localhost`. Maybe you're connecting to a remote database in the terminal? – Barmar Apr 09 '15 at 03:05
  • @Barmar is there a way to double check from the terminal? Because from Sequel Pro it says localhost/dobbletwo/... – Mazzone Apr 09 '15 at 03:07
  • 1
    SO has lots of questions related to that error: http://stackoverflow.com/search?q=%5Bmysql%5D+Can%27t+connect+to+local+MySQL+server+through+socket+%27%2FApplications%2FMAMP%2Ftmp%2Fmysql%2Fmysql.sock – Barmar Apr 09 '15 at 03:10
  • You need to update your question to include the `Can't connect to local MySQL server through socket` error text. – Simon MᶜKenzie Apr 09 '15 at 03:17
  • possible duplicate of [Can't connect to local MySQL server through socket](http://stackoverflow.com/questions/2182919/cant-connect-to-local-mysql-server-through-socket) – Simon MᶜKenzie Apr 09 '15 at 03:17
  • @SimonMᶜKenzie I don't think it's a duplicate of that question. While it's a similar error message, it doesn't look like the same cause as in the accepted answer. – Barmar Apr 09 '15 at 04:12
  • Good point, @Barmar - I should have read it more carefully. How about this one then? I can't flag it because I've used up my duplicate flag! http://stackoverflow.com/q/11657829/622391 – Simon MᶜKenzie Apr 09 '15 at 04:27
  • That one is for LAMP, not MAMP, so it's not clear how helpful the answers will be. – Barmar Apr 09 '15 at 04:37
  • 1
    connect to '127.0.0.1' instead of 'localhost' – pala_ Apr 09 '15 at 05:41
  • @SimonMᶜKenzie so I believe my question is different from [Can't connect to local MySQL server through socket](http://stackoverflow.com/questions/2182919/cant-connect-to-local-mysql-server-through-socket) that questions deals with mysql_connect() and mysql_real_escape_string() causing connection errors; mine is PDO so I believe the root of the problem is different (their issued seemed to arise from using mysql_escape_string()) -- besides that I don't know what changes the author made so I am not sure how similar those questions are – Mazzone Apr 09 '15 at 16:33

1 Answers1

5

the mysql libraries seem to interpret localhost as meaning you want to connect via a unix socket. your error indicates your mysqld isn't setup to accept unix socket connections.

to connect via tcp/ip instead, change define("DB_HOST", "localhost"); to define("DB_HOST", "127.0.0.1");

pala_
  • 8,901
  • 1
  • 15
  • 32
  • thanks a lot that worked! could you explain a little more why/ your logic? From the error you knew mysqld wasn't setup to accept unix socket connections... how did you know to go to tcp/ip? And what is the difference between the two? – Mazzone Apr 09 '15 at 16:23
  • 1
    MySQL really only has the two options - unix sockets, or tcp/ip. These days the default installation only listens on tcp/ip. The PHP libraries default to trying to connect via unix socket when you specify `localhost` as the address, and you're on a unix type OS (you seem to be on OSX). As for the differences - sockets are inter-process on the same machine, tcp/ip allows connections over the network. There's probably a slight speed advantage to sockets, but i doubt you'd ever notice it. – pala_ Apr 09 '15 at 23:09