TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-14-2007, 05:17 PM   #1 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Smile 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
meshi is offline  
Reply With Quote
Old 11-14-2007, 05:28 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-15-2007, 12:24 AM   #3 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

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
*/


?>
dschreck is offline  
Reply With Quote
Old 11-15-2007, 08:21 AM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

@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:

Feedback on OOP Classes
Tanax is offline  
Reply With Quote
Old 11-15-2007, 03:17 PM   #5 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default

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



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:45 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design