douyan6742 2018-05-14 19:14
浏览 58
已采纳

从配置变量连接到SQL导致错误(PHP代码)

I am using variables from config file to establish sql connection on my php files. Connection works fine in those files but to make the website more organized I decided to have one file for connection. I got the code from the internet, the connection does not worked. giving me this error:

' Error: Connect failed: Access denied for user 'something'@'server.com' (using password: NO)'

The user info at the config file is different and the password is set.

This is the details: My website like this structure:

root ----> 1)config.ini 2)public_html folder
public_html ----> website pages including 1)db_functions.php and 2)admin folder
admin ---->login.php and loginCode.php.

in the config.ini

[database]
username = someone
password = something
dbname = something

in db_functions.php file

<?php
/**
 * Database functions for a MySQL with PHP tutorial
 * 
 * @copyright Eran Galperin
 * @license MIT License
 * @see http://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17
 */

/**
 * Connect to the database
 * 
 * @return bool false on failure / mysqli MySQLi object instance on success
 */
function db_connect() {


    // Define connection as a static variable, to avoid connecting more than once 
    static $connection;

    // Try and connect to the database, if a connection has not been established yet
    if(!isset($connection)) {
        // Load configuration as an array. Use the actual location of your configuration file
        $config = parse_ini_file('../config.ini'); 
        $connection = mysqli_connect('server',$config['username'],$config['password'],$config['dbname']);
    }

    // If connection was not successful, handle the error
    if($connection === false) {
        // Handle error - notify administrator, log to a file, show an error screen, etc.
        return mysqli_connect_error(); 
    }
    return $connection;
}
?>

in loginCode.php

<?php

require_once('../db_functions.php');
$connection = db_connect();
      //if connection fails, stop script execution
      if (mysqli_connect_errno()) {
            $Message = "Error: " . $sql  .  "Connect failed: ". mysqli_connect_error();
        echo $Message;
      }
      else
          echo 'no error';
?>

Those cods have error: Error: Connect failed: Access denied for user 'something'@'server.com' (using password: NO)

However when I copied the same function from db_functions.php file to loginCode.php file and change this line $config = parse_ini_file('../config.ini'); to $config = parse_ini_file('../../config.ini'); , it works perfectly and the connection success. Also it works without error when I replaced the connection variables in db_functions.php by the actual database user information, calling the function from loginCode.php.

I think there is a problem on db_functions.php who can not assess information in config file but why the other files can access it?

update: I moved loginCode.php file to public_html folder connection works. and I moved db_functions.php file to admin folder it worked also.

  • 写回答

1条回答 默认 最新

  • duanfa2014 2018-05-14 20:07
    关注

    worked when I put both files loginCode.php and db_functions.php in the same folder.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?