Page 1 of 1

Query returns in procedural but not OOP

Posted: Thu Sep 07, 2006 4:21 pm
by RobertGonzalez
Ok, I am totally missing what is happening here. This works without incident...

Code: Select all

$sql = 'sp_help';
if ( !$result = sybase_query($sql, $link) )
{
	die('Could not show the tables: ' . sybase_get_last_message());
}
	
echo 'We have selected the tables!';
However, when I run that same query ($sql) through my class, I get an error returned:
Warning: sybase_query(): supplied argument is not a valid Sybase-Link resource in /path/to/page.php on line 197
Could not run procedure_name: ct_connect(): directory service layer: internal directory control layer error: Requested server name not found. on line 39
Here is the code for my query processor. What am I doing wrong?

Code: Select all

/**
 * Sends a database query to the DB server
 *
 * @param string $query
 * @param boolean $transacting
 */
function db_query($query = '', $transacting = false)
{
	// If there is an existing result, smack it down like a dog
	unset($this->query_result);
	
	// If the query is not empty, try to execute it
	if ( !empty($query) ) 
	{
		// Lets increment out query counter (Geek Feature 
		$this->query_count++;
		
		// Lets time the query (Some geek, some necessity)
		$start_time = $this->db_set_timer();
		$this->query_result = sybase_query($query, $this->db_link_id);
		$stop_time = $this->db_set_timer();
		
		// Actually increment the total query time
		$this->db_time_query($start_time, $stop_time);
	}
	
	// If we received a clean return from the database
	if ( isset($this->query_result) ) 
	{
		// Return it
		return $this->query_result;
	}/*
	else 
	{
		// Otherwise, check if we are in a transaction
		return ( $transacting == 2 ) ? true : false;
	}*/
}

Posted: Thu Sep 07, 2006 4:26 pm
by feyd
Where and how is $this->db_link_id set?

Posted: Thu Sep 07, 2006 4:30 pm
by Chris Corbyn
var_dump() is awesome :)

Posted: Thu Sep 07, 2006 4:31 pm
by RobertGonzalez
In the constructor, which according to my script is returning true.

Code: Select all

function DB_Sybase($server, $user, $password, $dbname, $charset = '', $appname = '')
	{
		// Constructor... Set some var values
		$this->dbserver = $server;
		$this->dbuser = $user;
		$this->dbpassword = $password;
		$this->dbcharset = $charset;
		$this->dbappname = $appname;
		$this->dbname = $dbname;
		
		// We are going to hack a connection string here because ...
		// sometimes charset may be used, sometimes not.
		$this->connection_string = $this->dbserver . ', ' . $this->dbuser . ', ' . $this->dbpassword;
		
		if ( !empty($this->dbcharset) )
		{
			$this->connection_string .= ', ' . $this->dbcharset;
			
			if ( !empty($this->dbappname) )
			{
				$this->connection_string .= ', ' . $this->dbappname;
			}
		}
		// ok, lets connect
		$this->db_link_id = @sybase_connect($this->connection_string);
		
		// If the connection worked AND there is a database name to select...
		if ( $this->db_link_id && !empty($this->dbname) ) 
		{
			// Make an attempt to select the database. If it fails...
			if ( !sybase_select_db($this->dbname, $this->db_link_id) ) 
			{
				// Close the connection because we can't use the connection without a database
				@sybase_close($this->db_link_id);
				return false;
			}
		}
		
		// Return the database server connection link identifier
		return $this->db_link_id;
	}

Posted: Thu Sep 07, 2006 4:38 pm
by Chris Corbyn
Don't return values from the contructor. They won't be passed back out when you make an object, the object will always have a TRUE value ;) Remove the @ for a second or add an isConnected() method :)

Posted: Thu Sep 07, 2006 4:46 pm
by RobertGonzalez
I removed the error suppression, which yielded some more information, such as...
Warning: sybase_connect(): Sybase: Server message: Changed database context to '<dbname>'. (severity 10, procedure N/A) in /path/to/phpfile on line 137
I removed the actual DB Name. I also changed the call to sybase_connect to this:

Code: Select all

$this->db_link_id = sybase_connect(DBHOST, DBUSER, DBPASSWORD);
Seeing that I totally overlooked the fact that I can use constants throught.

This just seems weird to me that I can call it directly but from the class I get those messages. I'll keep working on it. Thanks for the input so far. It is helpful.

Posted: Thu Sep 07, 2006 5:13 pm
by RobertGonzalez
Well, apparently the database messages are notice level when the deverity is less than 11. I am not sure why I am getting that notice when calling the class, but not when I call the functions directly, but I supposed I will need to work around this somehow, so I will research it a little more. Anyway, thanks for the help. Once again you guys have helped me out.

EDIT | In the interest of time (and because I am not wanting to be silly) I have downloaded and am using ADODBLite. Nice app, does what I want it to, is fast and works. Cool.