I am fairly new to OOP with php and i’m having a problem with my SQL connection im getting this error:
Warning: mysql_connect() [function.mysql-connect]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /home/.mantra/uername/myurl.com/includes/class_database.php on line 30
class database:
[code=php]
<?php
class database
{
public $host;
public $username;
public $password;
public $database;
function __construct($config)
{
$this->host = $config[‘db’][‘host’];
$this->username = $config[‘db’][‘username’];
$this->password = $config[‘db’][‘password’];
$this->dbname = $config[‘db’][‘database’];
}
function write()
{
echo $this->host.'<br/>’;
echo $this->dbname;
}
//connect to the database
function connect()
{
$conn = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
$SelDB = mysql_select_db($this->dbname);
return $conn;
}
//run a mysql command
function execute($strSQL)
{
$result = mysql_query($strSQL) or mysql_error();
return $result;
}
//count the number of rows
function row_count($result)
{
$row = mysql_num_rows($result);
return $row;
}
}
?>
[code=php]
<?php
class blog extends database
{
public $title;
public $body;
public $meta;
//construct the
function __construct($title=NULL, $body=NULL, $meta=NULL)
{
$this->connect();
$this->title = $title;
$this->body = $body;
$this->meta = $meta;
}
//add a new item to the blog
function insert_blog($_POST)
{
foreach ($_POST as $name=>$value)
{
$name = mysql_real_escape_string(htmlspecialchars($value));
}
$query = “INSERT INTO `blog` (title, text, meta, author)
VALUES (‘$title’, ‘$body’, ‘$meta’, ‘John Knowles’)”;
mysql_query($query) or die(mysql_error());
return ‘New Blog Created’.”nr”;
}
//update an item in the blog
function edit_blog($_POST)
{
foreach ($_POST as $name=>$value)
{
$name = mysql_real_escape_string(htmlspecialchars($value));
}
$query = “UPDATE `blog` (title, text, meta, author)
VALUES ($title, $body, $meta, ‘John Knowles’) WHERE id=’$id'”;
}
//display the blog on a page
function display_blog($num, $id=”, $search=”)
{
if (!empty($id))
{
$id = “WHERE id=’$id'”;
}
if (!empty($search))
{
$id = “WHERE text=’$search'”;
}
$query = “SELECT * FROM `blog` $id ORDER BY ‘date’ DESC LIMIT $num”;
$result = mysql_query($query) or die(mysql_error());
return $result;
}
}
?>
please be aware this was just a list of functions so it is not in any OOP state atm its a slow work in progress.
any help will be greatly appreciated (including links to external ooPHP/MySQL resources if anyone has any)