Jump to content

MySQL Connection Error in class def


jazzman247

Recommended Posts

Just now making the leap to OOP PHP and trying to connect to a MySQL DB inside the class and set object properties. but the connection fails. I think it must be some variable scope issue or maybe I can't connect inside a class?

 

my code looks something like this...

 

class Application {

static $dbServer  = "localhost";

static $dbUser = "user";

static $dbPassword = "pass";

static $dbName = 'dbname';

}

 

class User extends Application {

public $userId;

public $userType;

public $db_connection;

 

function __construct($userId)

{

// fails here

$db_connection = mysql_connect($dbServer,$dbUser,$dbPassword) OR die("Could not establish database connection");

// SELECT the database

mysql_select_db($dbName,$db_connection) or die("Could not get database");

 

}

 

 

My error:

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/ammaxpub/html/clsUsers.php on line 15

Link to comment
https://forums.phpfreaks.com/topic/180440-mysql-connection-error-in-class-def/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.