PHP function for mysqli connection gives error

纵然是瞬间 提交于 2019-12-20 05:01:10

问题


Newer to creating php functions and mysql. I have function to connect to a database db_conect_nm(). This is in file db_fns.php, and contains the user and password to connect to my db. I created this to have a more secure db connection. I had it in a directory outside of public_html, and got error PHP Warning: mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: (28000/1045): Access denied for user 'negoti7'@'localhost' (using password: NO) ...

Looking for solutions, I saw comments which indicated that perhaps this db user did not have permission from root, so I put it in a directory in public_html, same directory as program where it is being called. I still get the same error.

I have tested the connection without being a function, and it works. What is wrong, and why is this not working as a function? I really want to put this somewhere other than in the code directly and make it more secure.

db_fns.php content

<?php
//Database server
$host= 'localhost';
$nm_name= 'myname_databasename';  //sanitized data
$nm_user= 'myname_dbusername';
$nm_pword= 'password';

// db connect to nm database
function db_connect_nm()
{
   $nm_connect = new mysqli($host, $nm_user, $nm_pword, $nm_name);

   if (!$nm_connect)
     throw new Exception('Could not connect to NM database currently');
   else
   return $nm_connect; 
}

?>

I call it from nm_functions.php, db_fns.php is included there.

nm_functions.php

<?php require_once('sanitizedpathto/db_fns.php');

......some code

  $conn_nm = db_connect_nm();
  $result_sub = $conn_nm->query("select * from subscribers where uname='$username'");

  .... more code

?>

Any ideas? Thanks


回答1:


Could it be the scope of the variables? Have you tried defining the variables inside the function to test?

Like this:

<?php

// db connect to nm database
function db_connect_nm()
{
  //Database server
    $host= 'localhost';
    $nm_name= 'myname_databasename';  //sanitized data
    $nm_user= 'myname_dbusername';
    $nm_pword= 'password';

   $nm_connect = new mysqli($host, $nm_user, $nm_pword, $nm_name);

   if (!$nm_connect)
     throw new Exception('Could not connect to NM database currently');
   else
     return $nm_connect; 
}

?>




回答2:


You are :

  • first, declaring some variables, outside of any functions
  • then, trying to use those variables from inside a function.

Variables declared outside of a function are not, by default, visible from inside that function.

About that, you should read the Variable scope section of the manual.


Two possible solutions :

  • Use the global keyword, to import those variables into your function -- making them visible
  • Or define constants, instead of variables -- which makes sense, for configuration values.


In the first case, your function would look like this :

// db connect to nm database
function db_connect_nm()
{
   // Make these outside variables visible from inside the function
   global $host, $nm_user, $nm_pword, $nm_name; 

   $nm_connect = new mysqli($host, $nm_user, $nm_pword, $nm_name);

   if (!$nm_connect)
     throw new Exception('Could not connect to NM database currently');
   else
   return $nm_connect; 
}


And, in the second case, you'd first define the constants :

define('DB_HOST', 'localhost');
define('DB_DBNAME', 'myname_databasename');
define('DB_USER', 'myname_dbusername');
define('DB_PASSWORD', 'password');

And, then, use those constants :

// db connect to nm database
function db_connect_nm()
{
   $nm_connect = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DBNAME);

   if (!$nm_connect)
     throw new Exception('Could not connect to NM database currently');
   else
   return $nm_connect; 
}

This second solution is probably more clean than the first one ;-)




回答3:


Your connection variables are not scoped inside the connection function. You either need to pass them as parameters to the function (preferable) or use the global keyword inside to reference them:

function db_connect_nm($host, $nm_user, $nm_pword, $nm_name)
{
  $nm_connect = new mysqli($host, $nm_user, $nm_pword, $nm_name);

  //etc...
}

Alternate, not preferred:

function db_connect_nm()
{
  global $host, $nm_user, $nm_pword, $nm_name;
  $nm_connect = new mysqli($host, $nm_user, $nm_pword, $nm_name);

  //etc...
}


来源:https://stackoverflow.com/questions/5772064/php-function-for-mysqli-connection-gives-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!