Jump to content

[SOLVED] Script will not connect to MySQL -- Why?


dogfighter

Recommended Posts

I have a script that includes mysqlclass.php which will not connect to my database for some reason.

 

Here is the script up to the line where it dies:

 

<?php
include 'mysqlclass.php';

$server	= "localhost";
$user = "mydatabase_admin";
$pass = "swordfish";
$database = "mydatabase_name";

$mysql = new MySQL;
$mysql->config( $server, $user, $pass, $database );
$mysql->connect() or die("error" . mysql_error());

 

When I run it, it just prints "error" and does not include any mysql_error.

 

Here is mysqlclass.php:

 

<?php

class mysql {

function close(){

	return @mysql_close($this->conn);

}

function config( $server, $user, $pass, $database ){

	$this->database	= $database;
	$this->pass	= $pass;
	$this->server	= $server;
	$this->user	= $user;

	return true;

}

function connect(){

	$this->conn  = mysql_connect($this->server, $this->user, $this->pass)
			or die ("Unable to connect to Database Server");

	@mysql_select_db( $this->database, $this->conn )
			or die ("Could not select database");

}

function get_results(){

	return @mysql_fetch_array( $this->query );

}

function get_row(){

	return mysql_fetch_row( $this->query );

}

function query( $sql ){

                return $this->query = mysql_query($sql, $this->conn);

        }

function select_db(){

	mysql_select_db( $this->db_name ) or
       			$this->report_error('Error selecting MySQL database. Database name: '.$this->db_name."\r\n");

}

}

?>

 

I didn't write the script I'm just trying to fix it... any ideas what's killing it?

The connect function in the mysql class doesn't return any value so the line

 

$mysql->connect() or die("error" . mysql_error());

 

is the reason, the connect function already has error handling in it so why add this when calling the connect function ? if you remove the or die(...blah..) from the above line it should be fine, alternately you could rewite the connect function so it returns a value

also you declared you class as class mysql and you are calling with new MySQL... I think these are case sensitive. I could be wrong but I think your classes are case sensitive... so if you created it with all lower case then you must call it using lower case.

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.