Jump to content

mysql class


fantomel

Recommended Posts

hello i'm trying to build a mysql class from scratch first a primitive one then i will improve it :D

 

 

<?php
class MySQL
{
var $host;
var $name;
var $pass;
var $dba;
private $connection;

public function __construct($host, $name, $pass, $dba)
{
	if($host && $name && $pass & $dba) {
		if(!$this->connection = mysql_connect($host,$name,$pass))
		throw new MySQL_error("Database Connection Failed!");

		if(!mysql_select_db($dba, $this->connection))
		throw new MySQL_error("Database Can't Be Selected");
	} else {
		throw new Inter_error("Method: __construct--Error:Incorect arg(requires \$host, \$name, \$pass, \$dba)!!!!");
	}
}
public function Close()
{
	mysql_close($this->connection);
}

public function query() {
	return mysql_query($this->connection);
}
public function fetch_assoc(){
	return mysql_fetch_assoc($this->connection);
}
}
?>

that is my code for the moment.. and i wanted to test it a little bit see how it works .. stuff.. for the first three functions

 

 

but i run out on this error

Warning: mysql_fetch_assoc(): supplied resource is not a valid MySQL result resource in C:\webserver\sandbox.dev\public_html\classes\mysql.class.php on line 46

 

Please can someone help me to solve the problem.. posibly give me some advices ? thank you

Link to comment
Share on other sites

mysql_query and mysql_fetch_assoc...I could be wrong (highly doubt it) but these two functions pull data from the actual database. Why in God's name you are passing $this->connection in them is beyond me. Maybe reading up on the functions would help a little.

 

mysql_query: http://us2.php.net/mysql_query

 

mysql_fetch_assoc: http://us2.php.net/mysql_fetch_assoc

Link to comment
Share on other sites

<?php
class MySQL
{
   var $host;
   var $name;
   var $pass;
   var $dba;
   var $resultLink;
   private $connection;

   public function __construct($host, $name, $pass, $dba)
   {
      if($host && $name && $pass & $dba) {
         if(!$this->connection = mysql_connect($host,$name,$pass))
         throw new MySQL_error("Database Connection Failed!");
            
         if(!mysql_select_db($dba, $this->connection))
         throw new MySQL_error("Database Can't Be Selected");
      } else {
         throw new Inter_error("Method: __construct--Error:Incorect arg(requires \$host, \$name, \$pass, \$dba)!!!!");
      }
   }
   public function Close()
   {
      mysql_close($this->connection);
   }
   
   public function query($query) {
      $this->resultLink = mysql_query($query, $this->connection);
   }
   public function fetch_assoc(){
      return mysql_fetch_assoc($this->resultLink);
   }
}
?>

Link to comment
Share on other sites

hey sorry don't mean to discourage you but there's already a "mysql" class (and a "mysqli" class and a "PDO" class for that matter!)

 

definitely a good learning exercise to write a DB-interaction class but i'd read up on what's already available (maybe you just need to extend one of the existing classes?) and name it something different :)

Link to comment
Share on other sites

attached is a small script that tests SQL statements... its definitely not pretty and not optimized but its gets the job done which is: send a query and see the results.  I've found it useful to debug sql-related errors by echoing my $sql variable and then copy and pasting it into the script to see what happens... easier than var_dumping everything :)

 

im kind of tied up at work but if the OP is still having problems i'll be happy to check back and help later!

 

-Alex

 

[attachment deleted by admin]

Link to comment
Share on other sites

attached is a small script that tests SQL statements... its definitely not pretty and not optimized but its gets the job done which is: send a query and see the results.  I've found it useful to debug sql-related errors by echoing my $sql variable and then copy and pasting it into the script to see what happens... easier than var_dumping everything :)

 

im kind of tied up at work but if the OP is still having problems i'll be happy to check back and help later!

 

-Alex

 

very nice script and yes it's very useful for debuging :)

Link to comment
Share on other sites

fantomel, your code has changed so much from PMs to the thread that I'm lost!

 

Did you fix your problem?

 

And if not please post the entire class and test code that generates the error and of course the error itself!

 

I realize that there might be some hesitance in posting an entire class code here or anywhere for that matter (unless releasing under an open-source license) so if you must omit parts of your code try to omit the ones that aren't relevant to the problem at hand please!

 

Anyway, waiting on a reply!

 

Alex

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.