Do I need a php mysql connection in each function that uses database?

前端 未结 5 1011
无人共我
无人共我 2020-12-25 09:33

I am creating a php restful API and currently I have the database connection information in each function.

//Connect To Database
    $hostname=host;
    $use         


        
相关标签:
5条回答
  • 2020-12-25 09:48

    Create a config.php And add the code:

    config.php:

    $hostname = 'host';
    $username = 'username';
    $password = 'password';
    $dbname   = 'dbname';
    
    $conn = mysqli_connect($hostname, $username, $password) OR die('Unable to connect to database! Please try again later.');
    mysqli_select_db($conn, $dbname);
    

    Then in any file you wish to use mysql, add the following:

    script2.php

    <?php
    require_once 'config.php';
    
    mysqli_query($sqlApiAccess) or die('Error, insert query failed');
    ?>
    
    0 讨论(0)
  • 2020-12-25 09:52

    There is no need to make connection in each function. you need to make a connection file like conn.php and make the connection queries.

    <?php
    mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    ?>
    

    in any other file where you want to connect database just write this line

    <?php include("conn.php");?> 
    

    On this file you are able to run any query.

    0 讨论(0)
  • 2020-12-25 09:53

    do this:

    $db_connection= mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.');
    

    And every time you want to query:

    mysql_query("my_query",$db_connection);
    

    Note, if you connect to the DB in a function, you will need to global $db_connection.

    And when you want to close the DB connection:

    mysql_close($db_connection);
    
    0 讨论(0)
  • 2020-12-25 09:55

    To avoid creating a new database connection each time, we can use Singleton design pattern-

    we need to have a database class- to handle the DB connection-

    Database.class.php

    <?php
            class Database
            {
                // Store the single instance of Database
                private static $m_pInstance;
    
                private $db_host='localhost';
                private $db_user = 'root';
                private $db_pass = '';
                private $db_name = 'databasename';
    
                // Private constructor to limit object instantiation to within the class
                private function __construct() 
                {
                    mysql_connect($this->db_host,$this->db_user,$this->db_pass);
                    mysql_select_db($this->db_name);
                }
    
                // Getter method for creating/returning the single instance of this class
                public static function getInstance()
                {
                    if (!self::$m_pInstance)
                    {
                        self::$m_pInstance = new Database();
                    }
                    return self::$m_pInstance;
                }
    
                public function query($query)
                {
                   return mysql_query($query);
                }
    
             }
    ?>
    

    & we can call it from other files-

    other.php

    <?php
           include 'singleton.php';
           $pDatabase = Database::getInstance();
    
           $result = $pDatabase->query('...');
    ?>
    
    0 讨论(0)
  • 2020-12-25 09:59

    Why won't you move out the connection information into config and the call to mysql_connect into some factory?

    E.g.

    class ConnectionFactory {
        public static MysqlConnection CreateConnection() {
            $connection = mysql_connect(Config::$host, Config::$port etc);
            mysql_select_db($connection, Config::$schema);
            return $connection;
        }
    }
    

    and then in your code

    $connection = ConnectionFactory::CreateConnection();
    mysql_query($connection, $sqlApiAccess) or die('Error, insert query failed');
    
    0 讨论(0)
提交回复
热议问题