Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:53:16+00:00 2026-05-31T22:53:16+00:00

I am using functions to work with database.. Now the way i have defined

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:53:18+00:00Added an answer on May 31, 2026 at 10:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has Anyone tried using PDF in iPad Application, using CGPDf functions. I have used
I am using the SQLite database and have the following persistent class (simplified): public
I have a list of drop down lists populated form a database(using ASP.NET). These
I am trying to create a go-moku game using jquery,php, and mysql database. I
I have a generic Class I'm using to hold information loaded from a database.
We're using DI and Unity to work with different dependencies (generally, database and repository
Background: i was first asked to do a website using mysql as the database,
Using C#, I need to do some extra work if function A() was called
It is known that all functional languages share some basic properties like using functions
I know about map/reduce alghoritm and its use. It's using functions that are called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.