how do...
i thought it would be nice to abstract mysql functions to a class (as you do), and so the constructor of the class takes a db name and tries to connect to it. theres also an explicit deconstructor to close the connection.
now, this all worked fine on my pws/win95 development environment, but when i moved it to the real apache/linux server, it cries...
i've got a recursive function to generate html pages for a category hierarchy...
function generate($id) {
$db = new mysql("db");
$db->query("SELECT * WHERE ParentID = $id");
for($i = 0; $i < $db->numrows; $i++) {
print "some gubbins";
// recursively generate other stuff
generate($db->result("id", $i));
}
$db->destroy();
}
as you can see, if the function does recurse, more than one mysql object is created... this used to be no problem, but on apache/linux box i get the error
Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
even though I specify the correct port in the constructor of the mysql class...
class mysql {
var $result; // Result identifier of last query executed
var $numrows; // Number of rows in last query executed
var $dbid; // Connection ID
var $sql; // Last SQL statement executed
// Constructor (takes database name as argument)
function mysql($dbname) {
print "Trying to connect to $dbname<br>";
$this->dbid = mysql_connect("localhost:/var/lib/mysql/mysql.sock", "a_username");
mysql_select_db($dbname, $this->dbid);
print "Connected to $dbname<br>";
}
// Destructor (should be called when finished with object)
function destroy() {
mysql_close($this->dbid);
print "Disconnected<br>";
}
grumble grumble. does anyone know what's going on?
cheers
dom
🙂