Forum Moderators: coopster

Message Too Old, No Replies

My attempt to learn php better

         

impact

3:47 am on Jul 8, 2010 (gmt 0)

10+ Year Member



Hello,

This is my attempt to learn php better. I am confused which code would be more appropriate in terms of closing MYSQL connection.

function testCode($type,$username){
#Open database connection
$this->dbConnection();
# Security
$type = mysql_real_escape_string($type);
$username = mysql_real_escape_string($username);
# Perfom database connection
$query = mysql_query("SELECT * FROM account WHERE username = '$username' AND type = '$type'") or die (mysql_error());
$getHaveFun = $query['have_fun'];

if (empty ($getHaveFun) ){
header("Location:http://www.doamin.com/haveFunSetup");
}else{
if ($getHaveFun == 'male'){
$getMaleFunType = $query['getMaleFunType'];
header("Location:http://www.domain.com/havefun/male/".$getMaleFunType);
}else{
$getFemaleFunType == $query['getFemaleFunType'];
header("Location:http://www.domain.com/havefun/female".$getFemaleFunType);
}
}

mysql_close($this->con);
}




function testCode($type,$username){
#Open database connection
$this->dbConnection();
# Security
$type = mysql_real_escape_string($type);
$username = mysql_real_escape_string($username);
# Perfom database connection
$query = mysql_query("SELECT * FROM account WHERE username = '$username' AND type = '$type'") or die (mysql_error());
$getHaveFun = $query['have_fun'];

if (empty ($getHaveFun) ){
mysql_close($this->con);
header("Location:http://www.doamin.com/haveFunSetup");
die();
}else{
if ($getHaveFun == 'male'){
$getMaleFunType = $query['getMaleFunType'];
mysql_close($this->con);
header("Location:http://www.domain.com/havefun/male/".$getMaleFunType);
die();
}else{
$getFemaleFunType == $query['getFemaleFunType'];
mysql_close($this->con);
header("Location:http://www.domain.com/havefun/female".$getFemaleFunType);
die();
}
}

mysql_close($this->con);
}



Questions
- To reduce the duration of the database connection should I store all values from database into variables which I may or may not need.
- Should I remove the DIE() command from each line and therefore, place only 1 MYSQL_CLOSE command at the end of the function.
- Should I start and close mysql connection for each individual database queries?

If any thing else that you think may be beneficial to me, please do mention them.

Thank you,

Matthew1980

7:20 am on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there impact,

Useful link:[uk2.php.net ]

and I quote:-

Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().


So from that you can understand that you do not necessarily need to use the mysql_close($link); So if your code doesn't use this directive/function call, it wouldn't do any harm, whether this is a 'bad practise' issue coding wise - I'm not sure. Personally I think as it depends on the project and whether you have multiple connections for different reasons.

Also die(); and exit; will do exactly the same thing - terminate the script. It's a good idea, perhaps even good practice to use the exit; after the header, so that nothing downstream can be actioned and cause issues later on in the script.

I'm guessing that this is all in a class as you are using the self reference '$this'. Otherwise if this was outside the class, you would need to reference the class by using the $var you used to instanciate the class in the first place, though I guess you already knew that ;)

And for debugging requirements, I would build your sql queries outside the mysql_query(); function, so that you can easily echo the 'populated' string to screen should there be an error in the way as the sql is constructed, like this:-

$SqlQuery = "SELECT * FROM `account` WHERE `username` = '".$username."' AND `type` = '".$type."' ";
$getHaveFun = mysql_query($SqlQuery) or die(mysql_error());//only use this to debug not for release

so if you needed to debug that query, just add the echo to the beginning of the line:-
echo $SqlQuery = "SELECT * FROM `account` WHERE `username` = '".$username."' AND `type` = '".$type."' ";

I personally use this method, and as yet, no problems, I just find that SIMPLE queries dont need this method, only the queries that you have a variable in the mix ;)

Hope that this helps you a little,

Cheers,
MRb

impact

1:37 am on Jul 9, 2010 (gmt 0)

10+ Year Member



Wonderful wonderful. Thank you.

So closing mysql_close isn't all that necessary! ok, thank you.

$getHaveFun = mysql_query($SqlQuery) or die(mysql_error());//only use this to debug not for release

If this is so then how do you actually catch mysql errors? I mean if some thing goes wrong during run time.

Thank you.

Anyango

5:52 am on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can log those errors somewhere else, but if you display mysql_error during production, it wont be you who "catches" the error, it would be the visitor ;) and that looks bad.

Matthew1980

7:15 am on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Yes you can 'pipe' the errors generated (hopefully non!) into a file in a directory in your root folder of your domain, or wherever you prefer; there is usually a private_html folder in most hosts these days, so that you can periodically check that, that and just checking your server logs ;)

Read Rocknbils thought's in this thread:[webmasterworld.com ] gives a useful example of how to deal with error outputting to file, you can tweak the pseudo code to your needs, but you get the idea :)

>>but if you display mysql_error during production, it wont be you who "catches" the error, it would be the visitor ;) and that looks bad.

Agreed, this can give would be hackers essential information about the structure/names/table names of your databases, and that could potentially be fatal - if they know how to circumvent a form that posts/connects/inserts/updates whatever to your db.

This is why I commented saying only for debug not release :)

Hope this helps...

Cheers,
MRb