So I'm guessing this is not even close to being right, if what is it I am trying to do is doable. I am pretty new to php, comparitavely to many here, and I haven't yet written a working class that can be used at all, so I know I am not doing something right,

Im trying to write a database class that will create my connection to my mysql db in the first function, but i also want it to contain functions for all the common pieces of code used when querying the db, fetching the data, or updating, deleting data etc. That way I can just create an instance of the class, in other functions which will perform different tasks with data in the db, so with each new reference to the class, i can use the methods defined in it to do for example:

$db->new database();
$db->open();
$db->query("SELECT blah blah blah");

while (ssomething = $db->get_row() ) {

}

I have a feeling that I need a good amount more to this code but
here's what I have so far for the database class:

class database {

public $dbname	= 	'dbname'; //name
public $dbuser 	= 	  'dbuser'; // user
public $dbpass 	= 	 'dbpass'; // pass
public $dbhost 	= 	  'dbhost'; // host

public function open() {
	// select db
	mysql_select_db ($this->dbname) OR die ('Could not select database: ' . mysql_error() );
	// connect to db
	mysql_connect ($this->dbhost, $this->dbuser, $this->dbpass) 
		OR die ('Count not connect to MySQL: ' . mysql() );
}

public function query() {
	mysql_query();
}

public function num_rows() {
	mysql_num_rows($this->query() );

public function get_row() {
	mysql_fetch_array($this->query(), MYSQL_NUM);
}

public function close() {
	mysql_close();
}

}

And here's an example of how I'm trying to use it in a separate function that would get a list of categories for urls in my db:

require ('class.db.php');

function show_categories() {
	$db = new database();  // this should create a new instance so i think 
	$db->open();                 // opens the connection
	$db->query("SELECT * FROM link_cat ORDER BY category DESC");
	$output = '';                  // self explainatory i think
	while ($category = $db->get_row() ) {  // will fetch_array, or num_rows
		$cat_id = $category['link_cat'];
		$name = $category['category'];

	$output .= '<li><a href="category.php?cat=' . $cat . '">' . $name . '</a></li>\n';
}
$db->close();  // closes the connection
return $output;
}

So if anyone can help me figure out what I didn't do right, or just need to add, or if this is not possible this way exactly then please let me know how I would need to go about doing it otherwise,

Thanks alot

    Instead of writing a wrapper for the MySQL extension, use the MySQLi or PDO extension instead.

    but i also want it to contain functions for all the common pieces of code used when querying the db, fetching the data, or updating, deleting data etc.

    Suppose you choose to use the PDO extension. You can then extend this API by writing functions that take a PDO or PDOStatement object and perform "querying the db, fetching the data, or updating, deleting data etc".

      Just curious, is the database class I posted close to doing anything? Am I on the right idea as far as writing a class and then calling its method, or am I not even close, and the code I wrote is not going to doing anything, even if it were for something other than a db connection? I just want to know if I was on the right track in writing it. I feel like I was more than when writing other things I had no clue about. But I wouldn't know for sure...

      And with MySQLi, is that written the same way, but using mysqli_query() and other such functions, because I written a few small things using it before, but not enough to really see what the whole difference is.

      I am just looking to start learning how to use classes, because I know it is a better way to do things, and just want to start using them.

      With the code I posted, when I call the function "show_categories()" I get a message that says
      "Fatal error: Class 'database' not found in... " reffering to the line in the function "$db->new database();"

      So I thought maybe the class was missing something needed to work properly.

        Just curious, is the database class I posted close to doing anything? Am I on the right idea as far as writing a class and then calling its method, or am I not even close, and the code I wrote is not going to doing anything, even if it were for something other than a db connection? I just want to know if I was on the right track in writing it.

        Yes and no. Yes in the sense that you have the right idea for a wrapper class by forwarding the work to the underlying MySQL API. No in the sense that your implementation is not correct. For starters, you should have a member variable to keep track of the database connection resource, and then use this in every MySQL API call.

        And with MySQLi, is that written the same way, but using mysqli_query() and other such functions, because I written a few small things using it before, but not enough to really see what the whole difference is.

        MySQLi has support for both the procedural style functions (as in the MySQL extension) and an object based API (like what you are trying to do). PDO only provides an object based API, but it can be used for some database management systems other than MySQL.

          Just curious, is the database class I posted close to doing anything? Am I on the right idea as far as writing a class and then calling its method, or am I not even close, and the code I wrote is not going to doing anything, even if it were for something other than a db connection? I just want to know if I was on the right track in writing it. I feel like I was more than when writing other things I had no clue about. But I wouldn't know for sure...

          And with MySQLi, is that written the same way, but using mysqli_query() and other such functions, because I written a few small things using it before, but not enough to really see what the whole difference is.

          I am just looking to start learning how to use classes, because I know it is a better way to do things, and just want to start using them.

          With the code I posted, when I call the function "show_categories()" I get a message that says
          "Fatal error: Class 'database' not found in... " reffering to the line in the function "$db->new database();"

          So I thought maybe the class was missing something needed to work properly.

            Eh, your reply is the same as your previous post.

              I don't know why that was posted again, I didn't do it, I went out somewhere after the first one, and just got home. But sorry about that.

              Do you know of any sites that have more info about using msyqli?

              So far all I have found was examples of how to extend mysqli, which leads me to think that mysqli is a class in itself.. is that right? Also I saw some things that mention needing to download something that is used to connect to mysqli that is here:

              http://dev.mysql.com/downloads/connector/php-mysqlnd/

              I dont really understand what I have been able to find all too well, as far as how, what and where the differeneces are between mysql and mysqli when doing different things.

                Do you know of any sites that have more info about using msyqli?

                Read the PHP Manual on the MySQLi extension.

                So far all I have found was examples of how to extend mysqli, which leads me to think that mysqli is a class in itself.. is that right?

                Yes. That is why I recommended using MySQLi or PDO instead since writing your own lightweight object based database API wrapper would be like re-inventing the wheel.

                Also I saw some things that mention needing to download something that is used to connect to mysqli that is here:

                Basically, you need to enable the MySQLi extension. This could mean uncommenting a line in php.ini and restarting the webserver, or it could mean downloading and installing an OS package of some sort, etc.

                  laserlight wrote:

                  Basically, you need to enable the MySQLi extension. This could mean uncommenting a line in php.ini and restarting the webserver, or it could mean downloading and installing an OS package of some sort, etc.

                  Ok. So if you have your site hosted on a web host, say Godaddy, even though I hate them and don't use them, where you don't have access to the php.ini yourself scecifically, and don't control restarting the web server at all, unless you have your own server, I pretty much just need to have them do whatever is needed to enable it for me.

                  Thank you for your help, I appreciate it, I would rather not reinvent the wheel. Im going to work on making a mysqli db connection thing, and if I have any problems or questions later, you may see another post from me about it.

                  Thanks alot.

                    Ok. So if you have your site hosted on a web host, say Godaddy, even though I hate them and don't use them, where you don't have access to the php.ini yourself scecifically, and don't control restarting the web server at all, unless you have your own server, I pretty much just need to have them do whatever is needed to enable it for me.

                    That is true, but then that's what you are paying them to do 🙂

                      Write a Reply...