Jump to content

[SOLVED] Help with OOP


Andy-H

Recommended Posts

Ok, I dont actually have any code right now, I have been looking into OO PHP to build a MySQL class to handle connections, querys, fetching data etc. I have read a few tutorials with no success into understanding how to so this due to the tutorials all being written in shroom season and comparing OO PHP to magic boes and stuff :S DOes anyone know of a simple OOP database handeling script that I can pick apart abnd try to learn from?

 

Thanks for replies/help.

Link to comment
https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/
Share on other sites

From the tutorials I have read so far I got this far:

 


<?php
Class db
{
function db()
{
  $this->host = 'localhost';
  $this->user = '*****';
  $this->pass = '*****';
  $this->data = '*****';
   $conn = dbConnection();
}
function dbConnection
{
  $link = mysql_connect($this->host, $this->user, $this->pass)or die('Unable to establish link to db.');
  $conn = mysql_select_db($this->data, $link);
}

 

lol I assumes it was completely wrong and decided tutorials arent my style. I just perfer to edit the code, read the errors and fix them. Thats how I learned procedural anyway lol

Link to comment
https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726561
Share on other sites

Here's the class I use.

 

  		$host="***";
	$user_name="***";
	$password="***";
	$db="***";

	define("DB",$db);
	define("HOST",$host);
	define("USERNAME",$user_name);
	define("PASSWORD",$password); 

   class db_works
   {
	var $Query_ID=0;
	var $connection=0;

	function connect()
	{
		if($this->connection==0) 
		{
			$this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("Database Error
".mysql_error());
			$SelectResult = mysql_select_db(DB, $this->connection) or die("Could not Select Database".mysql_error());
		} 
		else 
		{
			echo "Database Connection Could not be Established";
			die();
		}
	}
   
	function query($sql) 
	{
		$this->Query_ID=mysql_query($sql,$this->connection);
		if(!$this->Query_ID)
		{
			$errorstr = mysql_error();
			if (stripos($errorstr, "Duplicate") === false)
			{
				echo "Query Failed " . $errorstr . "\n";
			}
		} 
		else return $this->Query_ID;
	}

	function connection_close()
	{
		mysql_close($this->connection);
	}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726565
Share on other sites

Here's the class I use.

 

  <?php
      $host="***";
      $user_name="***";
      $password="***";
      $db="***";
      
      define("DB",$db);
      define("HOST",$host);
      define("USERNAME",$user_name);
      define("PASSWORD",$password); 
      
   class db_works
   {
      var $Query_ID=0;
      var $connection=0;

      function connect()
      {
         if($this->connection==0) 
         {
            $this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("<b>Database Error</b><br>".mysql_error());
            $SelectResult = mysql_select_db(DB, $this->connection) or die("Could not Select Database".mysql_error());
         } 
         else 
         {
            echo "Database Connection Could not be Established";
            die();
         }
      }
   
      function query($sql) 
      {
         $this->Query_ID=mysql_query($sql,$this->connection);
         if(!$this->Query_ID)
         {
            $errorstr = mysql_error();
            if (stripos($errorstr, "Duplicate") === false)
            {
               echo "Query Failed " . $errorstr . " ";
            }
         } 
         else return $this->Query_ID;
      }

      function connection_close()
      {
         mysql_close($this->connection);
      }
   }
   
?>

 

Thanks, hopefully editing that will get me the basic understandings. If I get this right I'll post the code back later for anyone to critisize constructively or otherwise lol

Link to comment
https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726572
Share on other sites

Yeah that's just a basic SQL class that has some error checking.  But it's great if you have multiple servers and you forget the password, host etc...  This way you really can't make a mistake, just plop this file in an include section and connect.

Link to comment
https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726574
Share on other sites

Minor syntax errors, and you need to include the file.

 

require_once('db_works.php');
$db = new db_works();
$db->connect();
$query  = "SELECT blah1, blah2 FROM tblBlah ORDER BY timesUsed DESC LIMIT 10";
$result = $db->query($sql) or die(mysql_error());
?>

Link to comment
https://forums.phpfreaks.com/topic/138929-solved-help-with-oop/#findComment-726597
Share on other sites

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.