I have wrote this class in php from scratch there is no copy paste or anything like that I tried to create a base class for my projects and know I don't know if this is good
Here is my code and I don't know how to use it I know this sounds verry dumm but that the way it is
Thank you in adavance for any help tips or other feedback

//Dany 26.06.2012 
//Base Class
//@ to many
<?php
class baseClass {
//make them acessible to this class only
   private $dbHost="";
   private $dbUser="";
   private $dbPass="";
   private $dbName="";
   private $dbCon="";
   private $dbConError="";
   private $dbNameError="";

   //initialize the private var to be used with external ones  
   function __construct() {
        parent::construct();    

        $this->dbHost=$dbHost;
        $this->dbUser=$dbUser;
        $this->dbPass=$dbPass;
        $this->dbName=$dbName;
        $this->dbCon=$dbCon;
        $this->dbConError=$dbConError;
        $this->dbNameError=$dbNameError;

        $dbCon = mysql_connect($dbHost,$dbUser,$dbPass) or $dbConError = die("Can't connect");
        mysql_select_db($dbName) or $dbNameError = die("No database selected");
   }

   //error display 
   function Error($dbError) {
        if($dbConError) {
            echo $dbConError;
        }    

        if($dbNameError) {
            echo $dbNameError;    
        }

        if($errorSelect) {
            echo $errorSelect;
        }
        else {
            return false;
        }
   }

   //select query
   function select(){
      $result = mysql_query("") or $errorSelect=die("The query dose not exist");
      $num_rows = mysql_num_rows($result);
   }

   //display
   function display($num_rows){
      foreach ($num_rows as $key => $value) {
         echo $nume_rows .'</br>';
      }
   }
}
?>

There are some logic errors in this code. Before asking how to use it, you may need some guidance as how to build it too. First, where do the variables in the constructor come from? Shouldn't those be passed as parameters? Second, die() aborts your script, you cannot assign it to a variable.

from the private variables form the class and the die part your wright I can use a string and take out the die form there

I have made my modifcations

//Dany 26.06.2012 
//Base Class
//@ to many
<?php
class baseClass {
//make them acessible to this class only
   private $dbHost="";
   private $dbUser="";
   private $dbPass="";
   private $dbName="";
   private $dbCon="";
   private $dbConError="";
   private $dbNameError="";
   //initialize the private var to be used with external ones  
   function __construct() {
        parent::construct();    
        $this->dbHost=$dbHost;
        $this->dbUser=$dbUser;
        $this->dbPass=$dbPass;
        $this->dbName=$dbName;
        $this->dbCon=$dbCon;
        $this->dbConError=$dbConError;
        $this->dbNameError=$dbNameError;
        $dbCon = mysql_connect($dbHost,$dbUser,$dbPass) or $dbConError = "Can't connect";
        mysql_select_db($dbName) or $dbNameError = "No database selected";
   }
   //error display 
   function Error($dbError) {
        if($dbConError) {
            echo $dbConError;
        }    
        if($dbNameError) {
            echo $dbNameError;    
        }
        if($errorSelect) {
            echo $errorSelect;
        }
        else {
            return false;
        }
   }
   //select query
   function select(){
      $result = mysql_query("") or $errorSelect="The query dose not exist";
      $num_rows = mysql_num_rows($result);
   }
   //display
   function display($num_rows){
      foreach ($num_rows as $key => $value) {
         echo $nume_rows .'</br>';
      }
   }
}
?>

I want to implement this like this

<?php
require 'baseClass.php';
$base= new baseClass();
dbHost='localhost';
dbUser='user';
dbPass='12345';
dbName='database_name';
$base->select('SELECT * FROM cartoon_characters WHERE "cartoon" LIKE '%bugs%'');
$base->display();







?>

You should pass those settings to the constructor as parameters, it would be much neater. For the query, the method would have to change to this:

function select($query) {
    $result = mysql_query($query) or $errorSelect = "The query failed";
    $num_rows = mysql_num_rows($result);
}

The query is now passed to the method to be executed. The next problem is that $result and $num_rows are not accessible from your display function, unless you store them in a properties.

I get the feeling you're a little in over your head. You may want to read up on classes and objects first on php.net

I have read on php.net in the manul the objects and classes and I saw that I have to make $num_rows and $result public so that they can be acessed by the display function

function display($num_rows){
      foreach ($num_rows as $result => $value) {
         echo $result .'</br>';
      }
   }

I think this is it I had made the mistake of not using the result in the foreach
If this is not good please recomand a good tutorial reading on how to solve this
Thank you for your time

The private variables in the class are called properties. Anyway, although you could store the result and numRows, it would be better to put them into an array instead, so you can free the mysql result after you're done (thus freeing up resources). The numRows can then be easily taken from the array.

I can strongly advise you to have a good look at the manual.

I have read what you gave me it was really helpful but a lot of that stuff I new from the university in theory but not in a live example.
That's why I am probably cnfused about a lot of hands on aplications

OK you mean to create the array like this

$num_rows = mysql_num_rows[$result];

and then in the display use the foreach to get the rows printed to the screen

$num_rows = mysql_num_rows($result);

What I meant was this (but wanted you to try first):

function select($query) {
    $this->results = array (); // clear the results
    $result = mysql_query($query);
    if ($result) {
        $this->errorSelect = ''; // no error
        while ($row = mysql_fetch_array($result) {
            $this->results[] = $row;
        }
        mysql_free_result($result);
    }
    else {
        $this->errorSelect = 'The query failed';
    }
}

function display() {
    foreach ($this->results as $value) {
        echo $value['columnname'] . '</br>';
    }
}

You are really good can you point me where I can learn a hands on aproach on how to make my php skills grow faster and understand all that theory and pu t in practice
Thank you again for your time
You really are an expert

Thanks. I learned most of my stuff on PHP.NET, just going through the manual. Apart from this website, just practice a lot. The more you build, the more you learn.

One final question why does my variables are not recognized because this a mistery to me

Notice: Undefined variable: dbHost in C:\wamp\www\BaseClass\baseClass.php on line 17
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbUser in C:\wamp\www\BaseClass\baseClass.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbPass in C:\wamp\www\BaseClass\baseClass.php on line 21
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbName in C:\wamp\www\BaseClass\baseClass.php on line 23
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbCon in C:\wamp\www\BaseClass\baseClass.php on line 25
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbConError in C:\wamp\www\BaseClass\baseClass.php on line 27
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbNameError in C:\wamp\www\BaseClass\baseClass.php on line 29
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbHost in C:\wamp\www\BaseClass\baseClass.php on line 31
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbUser in C:\wamp\www\BaseClass\baseClass.php on line 31
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbPass in C:\wamp\www\BaseClass\baseClass.php on line 31
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

( ! ) Notice: Undefined variable: dbName in C:\wamp\www\BaseClass\baseClass.php on line 33
Call Stack
#   Time    Memory  Function    Location
1   0.0008  366016  {main}( )   ..\run.php:0
2   0.0016  383560  baseClass->__construct( )   ..\run.php:13

Your variables are not declared, like I suggested earlier, parameters would be better. It would look like this:

class baseClass {
   private $dbHost = "";
   private $dbUser = "";
   private $dbPass = "";
   private $dbName = "";

   function __construct($host, $user, $pass, $db) {
       $this->dbHost = $host;
       // etc.
   }
}

You would call it like this:

require 'baseClass.php';
$base = new baseClass('localhost', 'user', '12345', 'database');

Hope that I can help you someday with something as you help me This thread is solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.