Jump to content

[SOLVED] Connecting to MySQL


sneamia

Recommended Posts

I'm just starting off with OOP, and to be honest, no tutorials provide some applicable examples, so I decided to learn through trial and error.

 

I'm writing a basic MySQL class, and I'm running into this error.

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2) in /homepages/.../classes/mysql.php5 on line 10

 

index.php5:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
error_reporting(E_ALL);

require_once('classes/mysql.php5');
$result = new mysql();

unset($result);
?>
</body>
</html>

 

mysql.php5:

<?php
class mysql {
protected $hostname = 'xxxxxx.perfora.net';
protected $username = 'dboxxxxxxxx';
protected $password = 'xxxxxxxxx';
protected $dbname = 'dbxxxxxxx';
protected $mysqlconn;
protected $querystr;
function __construct($hostname = NULL, $username = NULL, $password = NULL, $dbname = NULL) {
	$mysqlconn = mysql_connect($hostname, $username, $password) or die('Could not connect: ' . mysql_error());
	mysql_select_db($dbname, $mysqlconn);
}

function query($querystr) {

}

function __destruct() {
	mysql_close($mysqlconn);
}
}
?>

 

Any thoughts?

Link to comment
Share on other sites

You should probably start with some tutorials or something.

 

<?php

class mysql {
  private $hostname = 'xxxxxx.perfora.net';
  private $username = 'dboxxxxxxxx';
  private $password = 'xxxxxxxxx';
  private $dbname = 'dbxxxxxxx';
  private $mysqlconn;
  private $querystr;
  
  function __construct() {
    $this->mysqlconn = mysql_connect($this->hostname, $this->username, $this->password) or die('Could not connect: ' . mysql_error());
    mysql_select_db($this->dbname, $this->mysqlconn);
  }
}

?>

Link to comment
Share on other sites

Thanks.

 

This works:

<?php
class mysql {
protected $mysqlconn;
function __construct($db_hostname = '******', $db_username = '******', $db_password = '******', $db_name = '*****') {
	$this -> mysqlconn = @mysql_connect($db_hostname, $db_username, $db_password) or die('Could not connect: ' . mysql_error());
	mysql_select_db($db_name, $this -> mysqlconn);
}

function query($querystr) {

}

function __destruct() {
	@mysql_close($this -> mysqlconn);
}
}
?>

 

 

edit by redbullmarky: be careful with leaving the login info in your code :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.