TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   connect database using oop (http://www.talkphp.com/absolute-beginners/1459-connect-database-using-oop.html)

meshi 11-14-2007 05:17 PM

connect database using oop
 
my head.php

PHP Code:

<?php
 
class mysql {

  function 
Connect($host$name$pass$db){

  
$connection mysql_connect("$host",
  
"$name",
  
"$pass");
  
mysql_select_db("$db"$connection);

  }
//ends the connection function

  
function Close(){

  
mysql_close($this->connection);

  }
//ends the close function

  

  
function FetchArray($query){
  
$rows mysql_fetch_array($query);
  return 
$rows;
  }

  function 
FetchNum($query){
  
$num mysql_num_rows($query);
  return 
$num;
  }

  function 
Query($sql){
  
$query mysql_query($sql) or die(mysql_error());
  return 
$query;
  }
//ends the query function

  
}//ends the class

?>

my test.php

PHP Code:

<?php
include("head.php");

  
$DB = new mysql();

  
$host "localhost";
  
$name "username";
  
$pass "password";
  
$db "database";

  
$connection $DB->Connect($host$name$pass$db);

  
//define an SQL statement and execute it
  
$sql "SELECT username FROM users";
  
$query $DB->Query($sql);

  
//output all rows from the statement
  
while($row $DB->FetchArray($query)){
  echo 
"<b>Username: $row[username]<b><br \>";
  
  }

  
//find the number of rows
  
$num $DB->FetchNum($query);
  echo 
"Number of rows: $num";

  
//close the connection
  
$DB->Close();

?>

it displays correct but there is a warning below..its stated :
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /web/head.php on line 15
why is it myql_close() seem not functioning.im a newbie of OOP..and im tried to connect to database using OOP.just practicing

Karl 11-14-2007 05:28 PM

You're getting the error because you're trying to access a property of the class ($this->conneciton) even though it has not yet been set.

Firstly, you should declare your $connection property as a member of the class:

Code:

class mysql {
    private $connection;
    ...

You then need to store the connection in the constructor when you first connect to the database, amend the code like this:

Code:

  function Connect($host, $name, $pass, $db){

  $this->connection = mysql_connect("$host",
  "$name",
  "$pass");
  mysql_select_db("$db", $this->connection);

  }

It's only a small change, basically we're just storing the connection as a property of the class now, so we can use it in other methods/functions of the class. Making these changes should sort that error out.

dschreck 11-15-2007 12:24 AM

Typed this up quickly... just an example of some things i noticed that may help you.
This is in PHP4, since that's what it looked like you were using..
But I left some comments for php5 usage.
Disclaimer: I didnt actually test the code, i just typed it. but no syntax errors, and everything looks like it should work. Again, just an example.
PHP Code:

<?php

// It looks like you're using PHP4, so this example is in PHP4.
// I'll leave notes if you are using PHP5, to as what you'd want to do.
// This is just an example of what you wish you want. and what you should do.

class mysql {
    var 
$connection// PHP5 : private $connection - this is our pointer
    
var $result;     // PHP5 : protected $result - this is our attribute that will store our results. Use this for debugging.
    
var $sql;        // PHP5 : protected $sql - this will store our query, for debugging.

    
function Connect($host$name$pass$db) { // PHP5 : public function
        
$this->connection mysql_connect($host,$name,$pass); // you dont need quotes around these items
        
mysql_select_db($db$this->connection);        
        return 
true;  // have a return type, so you know it didnt fail. 

    
}//ends the connection function

    
function Close(){ // php5 : public function
        
mysql_close($this->connection); 
        return 
true;
    }
//ends the close function


    
function FetchArray($query){ // php5 : public function    
        /* I added this if / else statement, to this method and the one below
           This will allow you to either pass it directly a SQL statement, or a resource object
           Ex:
               $sql = "SELECT * FROM Table WHERE 1=1";
               $get = $DB->FetchArray($sql);
               
               -- OR --
               
               $sql = "SELECT * FORM Table WHERE 1=1";
               $get = $DB->Query($sql);
               while($r = $DB->FetchArray($get)) {
                   ...
               }
        */
        
if(is_resource($query)) {
            
$rows mysql_fetch_array($query);
        } else {
            
$rows mysql_fetch_array($this->Query($query));
        }
        return 
$rows;
    }

    function 
FetchNum($query){
        if(
is_resource($query)) {
            
$num mysql_num_rows($query);
        } else {
            
$num mysql_num_rows($this->Query($query));
        }
        return 
$num;
    }

    function 
Query($sql){  // This is again, a public method, and it will be our most basic method,
        
$this->sql $sql;         // and our most important. This sets the SQL and Result parameters.
        
$this->result mysql_query($this->sql) or die(mysql_error());
        return 
$this->result;
    }
//ends the query function

}//ends the class

/* Hope this helps a little.. just wanted to show you how you can
    expand this in a way that will allow you to expand it  as your 
    projects grow. -dschreck
*/


?>


Tanax 11-15-2007 08:21 AM

@author of topic-

Although there's not much ACTUAL errors in your code, except the ones that the ppl above me pointed out, your code is not very nice.

And eventhough my db class isn't the best, you can still check it out and see if you get any inspiration:

http://www.talkphp.com/showthread.php?t=1134

meshi 11-15-2007 03:17 PM

thanks karl...the warning is gone now..and thanks dschreck for letting me understand the flow of the codes..more power guyz


All times are GMT. The time now is 01:04 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0