1

I am using functions to work with database.. Now the way i have defined the functions are as follows:-

/**
 * Database definations
 */
define ('db_type', 'MYSQL');
define ('db_host', 'localhost');
define ('db_port', '3306');
define ('db_name', 'database');
define ('db_user', 'root');
define ('db_pass', 'password');
define ('db_table_prefix', '');

/**
 * Database Connect
  */
function db_connect($host = db_host, $port = db_port, $username = db_user, $password = db_pass, $database = db_name) {
    if(!$db = @mysql_connect($host.':'.$port, $username, $password)) {
        return FALSE;
    }
    if((strlen($database) > 0) AND (!@mysql_select_db($database, $db))) {
        return FALSE;
    }
    // set the correct charset encoding
    mysql_query('SET NAMES \'utf8\'');
    mysql_query('SET CHARACTER_SET \'utf8\'');
    return $db;
}

/**
 * Database Close
  */
function db_close($identifier) {
    return mysql_close($identifier);
}

    /**
 * Database Query
  */
function db_query($query, $identifier) {
    return mysql_query($query, $identifier);
}

Now i want to know whether it is a good way to do this or not.....

Also, while database connect i am using

$host = db_host

Is it ok? Secondly how i can use these functions, these all code is in my FUNCTIONS.php The Database Definitions and also the Database Connect... will it do the needful for me...

Using these functions how will i be able to connect to database and using the query function... how will i able to execute a query?


VERY IMPORTANT: How can i make mysql to mysqli, is it can be done by just adding an 'i' to mysql....Like:-

@mysql_connect

@mysqli_connect
Django Anonymous
  • 2,987
  • 16
  • 58
  • 106
  • Re mysql -> mysqli: Best look at the examples in the manual. – Pekka Mar 25 '12 at 10:39
  • Pekka - OOps it is @mysqli_connect – Django Anonymous Mar 25 '12 at 10:43
  • 2
    Try using [PDO](http://php.net/manual/en/book.pdo.php) – Simone Mar 25 '12 at 10:49
  • @AbhilashShukla, what questions do you have? Do you mean that you wrote the above functions and you do not know how to use them? Weird! – Abhay Mar 25 '12 at 10:53
  • @Abhay i know that my question, i have defined some constants... as my database connection datas... now in the very next function i am using those constants in my function... i have never done that... so asking.. hope u'll help :) – Django Anonymous Mar 25 '12 at 10:57
  • 1
    Here are some tutorials that might help: [(1)](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) and [(2)](http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html) – tereško Mar 25 '12 at 11:15
  • @tereško thanks for the info... will go through it as have not been through PDO.. – Django Anonymous Mar 25 '12 at 11:22

1 Answers1

0

With global constants, you can directly use them inside a function. So in this case,

/**
 * Database Connect
 */
function db_connect() {
    if(!$db = @mysql_connect(db_host . ':' . db_port, db_user, db_pass)) {
        return FALSE;
    }
    if((strlen(db_name) > 0) AND (!@mysql_select_db(db_name, $db))) {
        return FALSE;
    }
    // set the correct charset encoding
    mysql_query('SET NAMES \'utf8\'');
    mysql_query('SET CHARACTER_SET \'utf8\'');
    return $db;
}

And here is how you can implement the client code:

<?php
if(!$conn = db_connect()) {
    die('cant connect to mysql');
}
$rs = db_query('SELECT * FROM tableA', $conn);
/* loop through the resultset */
db_close ($conn);
?>

Regarding converting mysql to mysqli, no it might not be that straight-forward; just adding an "i" might not work. MySQLi has different syntaxes. For one, the port is explicitly passed to the connect method rather than appended to the host as done above. Please read the manual.

You should also consider using PDO library as suggested by Simone Vittori.

Abhay
  • 6,545
  • 2
  • 22
  • 17
  • can you also tell me will i can make `@mysql` to `@mysqli`... unable to find it in php manual... – Django Anonymous Mar 25 '12 at 11:42
  • Of course you can make mysql to mysqli but the transition might not be that straight-forward. Also I will suggest using the object style of MySQLi rather than procedural style. I've given a link in my response – Abhay Mar 25 '12 at 12:06
  • The `@` (stfu operators) are not necessary. Checking the length of the DB name is a rather uncommon check. Setting the DB name can be done in the initial connection call. – mickmackusa Sep 20 '18 at 23:55