PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Sequalit
Forum Commoner
Posts: 75 Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas
Post
by Sequalit » Tue Nov 08, 2005 1:05 am
How does this look? Is there a better way to check if i have an open connection or not?
Code: Select all
class db{
var $username = "";
var $password = "";
var $database = "";
var $connected = 0;
function connect(){
mysql_connect(localhost, $username, $password);
@mysql_select_db($database) or die("The Database is currently unavaliable.");
$this->$connected=1;
}
function disconnect(){
mysql_close();
$this->$connected=0;
}
}
class core extends db{
var $data = "";
var $result;
function query($string){
$tempconn=0;
if($this->$connected==0){
$this->connect();
$tempconn=1;
}elseif($this->$connected==1){
if(is_numeric($string)){
$error = "Is numeric";
}
if(get_magic_quotes_gpc()){
$string = stripslashes($string);
}
$string = "'".mysql_real_escape_string(
html_entity_decode(
htmlspecialchars(
$string),ENT_NOQUOTES,"ISO-8859-1"))
."'";
$this->$result = mysql_query($string);
if(!$this->$result){
echo "An error has accured: <br />";
echo mysql_error();
}
$this->$data=$result;
}
if($tempconn == 1){
$this->disconnect();
$tempconn = 0;
}
}
function Row(){
$this->$data=mysql_fetch_row($this->$result);
}
function Array(){
$this->$data=mysql_fetch_array($this->$result);
}
function getData(){
return $this->$data;
}
function getResult(){
return $this->$result;
}
function free(){
$this->mysql_free_result($this->$result);
}
}
class member extends core{
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Nov 08, 2005 1:22 am
quote localhost.
Your class properties for the data, results, and so forth aren't being stored correctly:
is the correct way.
$result is not the same as $this->result or $this->$result. They cannot be arbitrarily switched amongs the others..
Sequalit
Forum Commoner
Posts: 75 Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas
Post
by Sequalit » Tue Nov 08, 2005 1:27 am
so to fix this problem i would do
Here is my updated code:
Code: Select all
class core extends db{
var $data = "";
var $result;
var tempconn = 0;
function query($string){
if($this->connected==0){
$this->connect();
$this->tempconn=1;
}elseif($this->connected==1){
if(is_numeric($string)){
$error = "Is numeric";
}
if(get_magic_quotes_gpc()){
$string = stripslashes($string);
}
$string = "'".mysql_real_escape_string(
html_entity_decode(
htmlspecialchars(
$string),ENT_NOQUOTES,"ISO-8859-1"))
."'";
$this->result = mysql_query($string);
if(!$this->result){
echo "An error has accured: <br />";
echo mysql_error();
}
$this->data=$this->result;
}
if($this->tempconn == 1){
$this->disconnect();
$this->tempconn = 0;
}
}
function Row(){
if($this->connected==0){
$this->connect();
$this->tempconn=1;
}elseif($this->connected==1){
$this->data=mysql_fetch_row($this->result);
}
if($this->tempconn == 1){
$this->disconnect();
$this->tempconn = 0;
}
}
function Array(){
if($this->connected==0){
$this->connect();
$this->tempconn=1;
}elseif($this->connected==1){
$this->data=mysql_fetch_array($this->result);
}
if($this->tempconn == 1){
$this->disconnect();
$this->tempconn = 0;
}
}
function getData(){
return $this->data;
}
function getResult(){
return $this->result;
}
function free(){
if($this->connected==0){
$this->connect();
$this->tempconn=1;
}elseif($this->connected==1){
$this->mysql_free_result($this->result);
}
if($this->tempconn == 1){
$this->disconnect();
$this->tempconn = 0;
}
}
}
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Nov 08, 2005 9:56 am
You're connecting and disconnecting every time a query is done. That could cause a lot of overhead if you end up using this class for lots of queries. I'd suggest connecting once when the object is created. That way, only one connection needs to be made per page. I wouldn't bother disconnecting after each query either - you may need the connection later - you could put the disconnection in your 'free()' function.
Same with 'Row()' - don't worry about disconnecting.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Sequalit
Forum Commoner
Posts: 75 Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas
Post
by Sequalit » Tue Nov 08, 2005 11:45 pm
alright, sounds good... am i doing the OOP correctly?
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Nov 09, 2005 10:00 am
I'm by no means an OOP guru, but technically speaking, your approach seems fine.
FYI: 'accured' should 'occured'
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Sequalit
Forum Commoner
Posts: 75 Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas
Post
by Sequalit » Wed Nov 09, 2005 5:45 pm
pickle wrote:
FYI: 'accured' should 'occured'
Lol... yeah I have a hard time spelling sometimes, especially when its really early in the morning and im living off the caffine i get from DrPepper's and GreenTea lol.
awsome, its good to know im doing it correctly, I have experience with Java so OOP is not new to me and i love it, but its a bit confusing in PHP... tehehe.
Sequalit
Forum Commoner
Posts: 75 Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas
Post
by Sequalit » Wed Nov 09, 2005 10:48 pm
okay those kind of confuse me... if i understand correctly
__sleep() puts your mysql connection on hold
__wake() puts your mysql connection on active duty
????
What exactly is serialize and unserialize for as well... from what i get they turn a varialbe into somethin like a session variable?
Sequalit
Forum Commoner
Posts: 75 Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas
Post
by Sequalit » Thu Nov 10, 2005 12:10 am
What about changing my connected variable to a global... so instead of doing this
do this implimentation, that way i can have several classes extending from Core/Db
Code: Select all
class db{
global $connected;
function connect(){
//blah
$GLOBALS['connected'] = 1;
}
}
That way in my index file, I can have a member class and a display news page
each would be extensions of Core
Code: Select all
$user = new member();
$page = new blogpage();
// all I would have to do is this right?
$user->connect();
//and I could do this
$page->query();
Also, what about making my actual object inside of GLOBALS, that way i can use it in each page?
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Thu Nov 10, 2005 3:48 am
No. Globals will not transfer from page to page, it is only used for when you want to access a variable out of scope, not on different pages.
serialize() transforms your object into a string that can be stored in a session variable, database, blah blah blah
unserialize then restores the stored string to it's former glory, providing the class definitition is included before you try to unserialize() the object.
Try it.