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?

Link to comment
Share on other sites

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

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.