Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 09-28-2005, 06:58 PM sql connect class
Experienced Talker

Posts: 38
Location: Holland
Trades: 0
hey

i saw on a site a php class like this:

PHP Code:
<?php

class db_sql {

var 
$dbserver="";
var 
$user="";
var 
$pass="";
var 
$name="";
var 
$array=array();

    function 
connect() {
    
$conn=mysql_connect($this->dbserver,$this->user,$this->pass) or $this->db_die();
    return 
$conn;
    }

    function 
db_query($query) {
    
$get=mysql_db_query($this->name,$query,$this->connect()) or $this->db_die();
    list(
$result)=mysql_fetch_row($get);
    return 
$result;
    }

    function 
num_vals($query) {
    
$get=mysql_db_query($this->name,$query,$this->connect()) or $this->db_die();
    
$this->array=array();
        while(
$row=mysql_fetch_array($get)) {
        
$this->array[]=$row[0];
        }
    return 
$this->array;
    }

    function 
mul_vals($query) {
    
$get=mysql_db_query($this->name,$query,$this->connect()) or $this->db_die();
    
$this->array=array();
    
$this->array=mysql_fetch_array($get);
    return 
$this->array;
    }

    function 
ins_vals($query) {
    
$do=mysql_db_query($this->name,$query,$this->connect()) or $this->db_die();
    }
    
    function 
db_die() {
    die(
"Mysql error");
    }
}

?>

i wanted to use a connection class like this one, but i was wondering.. if i am correct i see that for every function a new connection is used.
or am i wrong here?

if i am right, isnt it better to just make 1 connection and make the querys from 1 database without the new connections in the functions over and over again?

that was my question.

greetings
morningstar
excalibur112 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-28-2005, 07:22 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Yes it does look like a new connection is made every time....

If it were me, I'd make $conn into a member variable of the class:
PHP Code:
<?php

class db_sql {

var 
$dbserver="";
var 
$user="";
var 
$pass="";
var 
$name="";
var 
$array=array();
var 
$conn;

    function 
connect() {
    
$this->conn=mysql_connect($this->dbserver,$this->user,$this->pass) or $this->db_die();
    }

    function 
db_query($query) {
    
$get=mysql_db_query($this->name,$query,$this->conn) or $this->db_die();
    list(
$result)=mysql_fetch_row($get);
    return 
$result;
    }

    function 
num_vals($query) {
    
$get=mysql_db_query($this->name,$query,$this->conn) or $this->db_die();
    
$this->array=array();
        while(
$row=mysql_fetch_array($get)) {
        
$this->array[]=$row[0];
        }
    return 
$this->array;
    }

    function 
mul_vals($query) {
    
$get=mysql_db_query($this->name,$query,$this->conn) or $this->db_die();
    
$this->array=array();
    
$this->array=mysql_fetch_array($get);
    return 
$this->array;
    }

    function 
ins_vals($query) {
    
$do=mysql_db_query($this->name,$query,$this->conn) or $this->db_die();
    }
    
    function 
db_die() {
    die(
"Mysql error");
    }
}

?>
It's also inefficient in that if you want to know the number of rows returned for a query, it re-runs the query!

Beware, the only OO type coding I've done any great amount of is JAVA and C++, so the above code may have some problem that I've missed.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-28-2005, 07:39 PM
Experienced Talker

Posts: 38
Location: Holland
Trades: 0
thanx for your quick response oberon

still i have a question about that member variable. I dont really know what the meaning of it.

If i add $conn; to the member variables then what will be the change to every connection i make all the time? will it store the connection in the memory of the computer or something, so it will load faster.. or i really dunno

greets morningstar.

Last edited by excalibur112; 09-28-2005 at 07:42 PM..
excalibur112 is offline
Reply With Quote
View Public Profile
 
Old 09-29-2005, 04:36 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
If it's a member variable of the class, then once you have an instance of the calss (ie you say $dbsql = new db_sql() Then that class instance occupies some memory on the heap. $conn used to be just returned from the connect function, and recreated every time. Now the connect function connects to the database, and instead of returning $conn it sticks it into the member variable I added. When you run a query it looks for $conn and uses that rather than recreating a connection. (Notice all the other functions now have $this->conn instead of $this->connect() )
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-29-2005, 05:12 AM
Experienced Talker

Posts: 38
Location: Holland
Trades: 0
ah ok i understand it now

my question/problem is solved now, thanx for your response

greets
excalibur112 is offline
Reply With Quote
View Public Profile
 
Old 09-29-2005, 12:24 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
A new connection would not be created each time. Though the connect() method is being invoked each time a connection is required, the mysql_connect() function will return the same resource. From the PHP manual, under the explanation of the new_link parameter:
Quote:
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
So you won't end up with a bunch of connections. Albeit, it is still an inefficient way to do it.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 09-29-2005, 01:39 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Ah, OK so I had the OO bit right, but not the mysql_connect() part...
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-30-2005, 08:12 AM
Experienced Talker

Posts: 38
Location: Holland
Trades: 0
if this mysql class is such an ineffective, you maybe have an url to one that is good and effective. Or could you give me a preview?

(to count rows i dont really need to run a new query, i just do sizeof().. )

thanx, greets morningstar
excalibur112 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to sql connect class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26509 seconds with 12 queries