Forum Moderators: open

Message Too Old, No Replies

Opening a database in PHP

Is this way most effiecient?

         

joshm

2:56 pm on Oct 31, 2006 (gmt 0)

10+ Year Member



Hi,
I am making a php search page that will get a lot of visitors so I want to make sure i'm opening the database in the most efficient way. I have this code at the very start of my page before any html:

<?php
$mysql_host = "localhost";
$mysql_user = "username";
$mysql_pass = "password";
$mysql_db = "database";
$mysql_access = mysql_pconnect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysql_db, $mysql_access);
?>

Then near the middle of the page, amongst all the html I have:

<?php
mysql_query("INSERT INTO search SET q='" . $q . "'", $mysql_access);
?>

And finally, at the very bottom after the end html tag, I have:

<?php
mysql_close($mysql_access);
?>

Now, I have no idea if this is a good way of doing it or not, i'm new to setting up this kind of stuff. I do know it works because the test query being searched is being inserted into the database. Being a search page and all, it will generate a lot of traffic so I don't want to kill the server because of my poor coding :P

Is this the most efficient way of opening a database? Any help much appreciated.

RonPK

8:09 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$mysql_host = "localhost";
$mysql_user = "username";
$mysql_pass = "password";
$mysql_db = "database";
$mysql_access = mysql_pconnect($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db($mysql_db, $mysql_access);

Unless you intend to re-use them, there is no need to create all those variables. They only take up memory space. This should do:

$mysql_access = mysql_pconnect('localhost', 'username', 'password');  
mysql_select_db('database', $mysql_access);

Note the use of single quotes: they require less processing power as the parser will not search single-quoted strings for variables. No one will notice, but everything helps...

joshm

1:12 am on Nov 3, 2006 (gmt 0)

10+ Year Member



What you said makes sense because I will not be re-using the variables.

Note the use of single quotes: they require less processing power as the parser will not search single-quoted strings for variables. No one will notice, but everything helps...

I didn't even know this! I have a very large site where I am using double quotes in some of my code, so thanks for the tip.

eelixduppy

3:35 am on Nov 3, 2006 (gmt 0)




I didn't even know this! I have a very large site where I am using double quotes in some of my code, so thanks for the tip.

Always check the documentation [us2.php.net] for more details ;)