duanjiao6730 2016-05-07 11:57
浏览 33

PHP函数和数据库连接

I'm trying to keep one file for PHP connection instead of using mysqli_connect on every page.

I have a config.ini file which has the Db credentials, this file i call in a php file db_connect.php The code of db_connect.php is below:

//stores hostname or the server name
$hostName;
//username for the database
$userName;
//password for the database
$password;
//database name
$dbName;

$ini = parse_ini_file('../../config/config.ini');   
while(list($key,$value) = each($ini))
{
    if($key == 'hostName')
    {
        $hostName = $value; //retrieving value of host name.
    }
    else if($key == 'userName')
    {
        $userName = $value; //retrieving database user name
    }
    else if($key == 'password')
    {
        $password = $value; //retrieving database password
    }
    else if($key == 'dbName')
    {
        $dbName = $value;   //retrieving database name.
    }
}
$connectobject1 = new mysqli($hostName, $userName, $password, $dbName);
if ($connectobject1->connect_errno) 
{
    echo "Connection Failed";
    exit();
}

using the above code, i'm able to create a connection to the database. Now when i include db_connect.php in my index.php file, i use require_once 'db_connect.php' however the problem starts when in index.php my functions execute. These functions are not able to access the connection string. a sample of my index.php code is below:

//Creating DB connection
require_once '../connect/db_connect.php';

//Checking DB connection
if(!$connectobject1) 
{
    //connection to DB failed
    header('Location: /pages/error/error.php?id=0'); 
    exit();
}

function checkuser($UserID, $connectobject1)
{

    //function code
}

I've read that it is not a good practice to use global for connections.

How can I proceed with my connections?

  • 写回答

1条回答 默认 最新

  • duanhao4156 2016-05-07 12:01
    关注

    use a file like this..

    config.php file which will have server details

     <?php
    
        // PHP Database Constants
        // Database Constants
            defined('DB_SERVER') ? null : define("DB_SERVER", "127.0.0.1");
            defined('DB_PORT') ? null : define("DB_PORT", "8889");
            defined('DB_USER')   ? null : define("DB_USER", "popat234");
            defined('DB_PASS')   ? null : define("DB_PASS", "4JfQuuQnzU6yfPdm");
            defined('DB_NAME')   ? null : define("DB_NAME", "demo");
    
        ?>
    

    database.php which will have the connection settings

    <?php
    
    require_once ('config.php');
    
    class MYSQLDatabase {
    
        private $connection;
    
      function __construct() {
        $this->open_connection();
      }
    
      public function open_connection() {
        $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME, DB_PORT);
        if(mysqli_connect_errno()) {
          die("Database connection failed: " . 
               mysqli_connect_error() . 
               " (" . mysqli_connect_errno() . ")"
          );
        }
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错