May 23, 2009 16

PHP programmer’s evolution scribble

By admin in php

The various stages you go through in your development career.

$mysql = mysql_connect('localhost', 'reinhold', 'secret_hash');
mysql_select_db('wordpress') or die("cannot select DB");

Trying a DRY approach

$db_host = 'localhost';
$db_user = 'reinhold';
$db_password = 'secret_hash';
$db_database = 'wordpress';
 
$mysql = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_database);

As the values normally don’t change, how about using constants

define('DB_HOST', 'localhost');
define('DB_USER', 'reinhold');
define('DB_PASSWORD', 'secret_hash');
define('DB_DATABASE', 'wordpress');
 
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

After years of changing the values every time you uplaod something to the live server

define('LIVE_ENV', true);
 
if(LIVE_ENV) {
	define('DB_HOST', 'localhost');
	define('DB_USER', 'reinhold');
	define('DB_PASSWORD', 'secret_hash');
	define('DB_DATABASE', 'wordpress');
} else {
	define('DB_HOST', 'testserver.com');
	define('DB_USER', 'reinhold');
	define('DB_PASSWORD', 'secret_hash');
	define('DB_DATABASE', 'wordpress');	
}
 
$mysql = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

Even better would be this

if ($_SERVER["HTTP_HOST"] == 'www.domain.com')  // remote live environment
{ ... }
else // localhost test environment
{ ... }

PHP5 procedural approach using the new mysqli extension

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
 
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
printf("Host information: %s\n", mysqli_get_host_info($link));
mysqli_close($link);

Tags: ,

16 Responses to “PHP programmer’s evolution scribble”

  1. [...] Achei interessante e estou compartilhando com vocês um artigo que traduzi do blog de um desenvolvedor web Alemão: Reinhold Weber. [...]

  2. [...] Achei interessante e estou compartilhando com vocês um artigo que traduzi do blog de um desenvolvedor web Alemão: Reinhold Weber. [...]

  3. [...] DRY stands for Don’t Repeat Yourself, and it’s a valuable programming concept, no matter what the language. DRY programming, as the name implies, is ensuring that you don’t write redundant code. Here’s an example from Reinhold Weber: [...]

  4. [...] built-in function for that 100 lines of code, try combining your statements, and so on – here is a very good tutorial on this topic. Once you think that your code is perfect, it’s time to [...]

  5. [...] This post was Twitted by JonGauthier [...]

  6. [...] This post was Twitted by zamshed [...]

  7. [...] 40 Tips for optimizing your php code [...]

  8. [...] This seems like an obvious thing, but when working on multiple projects, over time you start to see ways you can improve your most used snippets of code. As a PHP developer I frequently use mysql_connect(), but have gotten to the point I dont even think about it as I have it included at the top of all my scripts. I just need to change the constants to define username, password, and database for the various projects I use it on. This can be further improved by adding conditional statements to differentiate between live and developement environments. This is an excellent example of evolving code. [...]

  9. [...] »优化PHP代码的40条建议原文作者:Reinhold Weber 原文链接:40 Tips for optimizing your php Code [...]

  10. Cybril says:

    Just a word of advice about using the check:

    if ($_SERVER["HTTP_HOST"] == ‘www.domain.com’)

    you need to also check for ‘domain.com’ (so without the www) otherwise if the DNS records aren’t set up properly and people type your url without the www and you have the above check in place, they will get an error message because the ‘else’ part of the check will run (thereby trying to connect to your local environment)

  11. westworld says:

    I use a Class with ini files for this.
    class DB_Pdo extends PDO{
    function __construct($name=’mydb’){
    switch ($name){
    case ‘mydb’:
    $settings = parse_ini_file(“config/mydb.ini”);
    break;
    case ‘otherdb’:
    $settings = parse_ini_file(“config/otherdb.ini”);
    break;
    default:
    echo “no database selected”;
    exit;
    }
    $user = $settings['username'];
    $password = $settings['password'];
    $hostname= $settings['hostname'];
    $database= $settings['database'];
    $driver= $setting['driver'];
    parent::__construct(“mysql:dbname=$database;host=$hostname”,$user,$password);

  12. A very useful article. I am a newbie with PHP coding, hence have been referring your articles often.

    Also a big thank you to westworld. :)

  13. Good work, hope to hear more from you.Are you working in a Group that you can make such a goog Script?

  14. keyur patel says:

    nice article, thanks

  15. Keep writing, I simply can’t get enough. You’re doing a standup job, and I thank you for it.

  16. [...] 40 Tips for optimizing your PHP code [...]

Leave a Reply

Comment Spam Protection by WP-SpamFree