[PHP/SQL] trouble with class/functions

Hey there,

I’m having mega compatibility issues with the CMS I’ve been writing for a university assignment.

I’ve basically programmed the whole cms inside my own webspace and only recently uploaded it to my university webspace.

Basically, the university server i’m using doesn’t like me including mysql_connect inside a function. This causes me big troubles since the majority of my scripts rely on the function being included.

It throws me this error:
**Warning: mysql_query() [function.mysql-query]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in /var/www/student/th4ect/latestNews.php on line 26

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/student/th4ect/latestNews.php on line 26
Couldn’t Execute Query

**It all works smoothly if outside a function.

I’ll paste my db connection script for good measure: (well not mine :liar:).


<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';

class DbConnector extends SystemComponent {

var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){

    // Load settings from parent class
    $settings = SystemComponent::getSettings();

    // Get the main settings from the array we just loaded
    $host = $settings['dbhost'];
    $db = $settings['dbname'];
    $user = $settings['dbusername'];
    $pass = $settings['dbpassword'];

    // Connect to the database
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));

}

//*** Function: query, Purpose: Execute a database query ***
function query($query) {
    $this->theQuery = $query;
    return mysql_query($query, $this->link);
}

//*** Function: getQuery, Purpose: Returns the last database query, for debugging ***
function getQuery() {
    return $this->theQuery;
}

//*** Function: getNumRows, Purpose: Return row count, MySQL version ***
function getNumRows($result){
    return mysql_num_rows($result);
}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {
    return mysql_fetch_array($result);
}

//*** Function: close, Purpose: Close the connection ***
function close() {
    mysql_close($this->link);
}


}
?>

According to peeps in another forum, I should be looking at my ‘Scope Issues’. Can anyone help me with this 'ere problem, please?

Thanks in advance,
Tom

Possibly the $settings array you are receiving from SystemComponent::getSettings(); is not being populated, hence not establishing a connection to the database.

Check the php versions on both. Yours is written for php 4, whereas your university webspace may be php 5. Objects have changed quite a bit since php 4 so there may be some issues with the static function call.

To debug do a print_r() on your settings to see if the array contains the required connection data. Also, do a print_r() on the dbconnector link member, check to see if it points to a mysql resource id#