Jump to content

Recommended Posts

 

The below is giving me the following:

 

 

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /home/zyquo/public_html/beta/db_select.php on line 53

Connect Error (1045) Access denied for user 'root'@'localhost' (using password: NO)

 

$DBUsername and $DBPassword are set at the top of the file and they're correct.

 

class mysqli_errordisplay extends mysqli {
    public function __construct($host, $user, $pass, $db) {
        parent::__construct($host, $user, $pass, $db);

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

function MysqliPersist($dbname){
$DBconnect = new mysqli_errordisplay('p:localhost',  $DBusername, $DBpassword, $dbname);
return $DBconnect;
}
function MysqliCOE($dbname){
$DBconnect = new mysqli_errordisplay('localhost',  $DBusername, $DBpassword, $dbname);
return $DBconnect;
}

Link to comment
https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/
Share on other sites

Alright, solved with setting the variables to global. Now I'm getting the following warning:

 

Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/zyquo/public_html/beta/db_select.php on line 71

 

Line 71:

$DBconnect = new mysqli_errordisplay('localhost', $DBusername, $DBpassword, $dbname)';

 

 

full code:

class mysqli_errordisplay extends mysqli {
    public function __construct($host, $user, $pass, $db) {
        parent::__construct($host, $user, $pass, $db);

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

function MysqliPersist($dbname){
global $DBusername, $DBpassword;
$DBconnect = new mysqli_errordisplay('p:localhost',  $DBusername, $DBpassword, $dbname);
return $DBconnect;
}
function MysqliCOE($dbname){
global $DBusername, $DBpassword;
$DBconnect = new mysqli_errordisplay('localhost', $DBusername, $DBpassword, $dbname)';
}

 

EDIT: Crap. Had a quote on the end. I seem to always solve my own posts...

I tried that and it didn't work.

 

function MysqliPersist($dbname, $DBUsername, $DBpassword){
global $DBusername, $DBpassword;
$DBconnect = new mysqli_errordisplay('p:localhost',  $DBusername, $DBpassword, $dbname);
return $DBconnect;
}

 

Gives me an error when I use MysqliPersist("some_db_name"); in the actual page. It says something along the lines of argument 2 and 3 are invalid or missing.

 

The whole point of the function is so the other coders on the team don't need to know the mysql login details.

 

Is there another way to pass the login details to the argument?

 

I also tried:

 

function MysqliPersist($dbname, $DBUsername=$DBusername, $DBpassword=$DBpassword){
global $DBusername, $DBpassword;
$DBconnect = new mysqli_errordisplay('p:localhost',  $DBusername, $DBpassword, $dbname);
return $DBconnect;
}

When you pass in arguments, you need to remove the line that has 'global'.  As in:

 

function MysqliPersist($dbname, $DBusername, $DBpassword) {
   $DBconnect = new mysqli_errordisplay('p:localhost', $DBusername, $DBpassword, $dbname);
   return $DBconnect;
}

 

You also need to ensure that those values actually exist before invoking the function.  Example:

 

$dbname = "MyAppDB";
$username = "Bubba";
$pass = "Forrest Gump";

$connect = MysqliPersist($dbname, $username, $pass);

When you pass in arguments, you need to remove the line that has 'global'.  As in:

 

function MysqliPersist($dbname, $DBusername, $DBpassword) {
   $DBconnect = new mysqli_errordisplay('p:localhost', $DBusername, $DBpassword, $dbname);
   return $DBconnect;
}

 

You also need to ensure that those values actually exist before invoking the function.  Example:

 

$dbname = "MyAppDB";
$username = "Bubba";
$pass = "Forrest Gump";

$connect = MysqliPersist($dbname, $username, $pass);

 

Yep, that's pretty much exactly what I had Before I switch it to global. I used global after trying it that way.

 

In the example I posted, I forgot to remove the globals. I just copied what I currently have and changed the function argument.

 

Here's the function with variables:

$DBusername="user";
$DBpassword="password";

class mysqli_errordisplay extends mysqli {
    public function __construct($host, $user, $pass, $db) {
        parent::__construct($host, $user, $pass, $db);

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

function MysqliPersist($dbname, $DBusername, $DBpassword){
$DBconnect = new mysqli_errordisplay('p:localhost',  $DBusername, $DBpassword, $dbname);
return $DBconnect;
}
function MysqliCOE($dbname, $DBusername, $DBpassword){
$DBconnect = new mysqli_errordisplay('localhost', $DBusername, $DBpassword, $dbname);
return $DBconnect;
}

 

 

This is what I get in return:

 

 

Warning: Missing argument 2 for MysqliCOE(), called in /home/zyquo/public_html/beta/test.php on line 3 and defined in /home/zyquo/public_html/beta/db_select.php on line 69

 

Warning: Missing argument 3 for MysqliCOE(), called in /home/zyquo/public_html/beta/test.php on line 3 and defined in /home/zyquo/public_html/beta/db_select.php on line 69

 

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /home/zyquo/public_html/beta/db_select.php on line 55

Connect Error (1045) Access denied for user 'root'@'localhost' (using password: NO)

That's what I posted in my last post:

 

This is line 53:

class mysqli_errordisplay extends mysqli {

 

this is line 71:

 

return $DBconnect;

 

 

The php file calling the function is simply this right now:

 

<?php
include 'db_select.php';
MysqliCOE("db_name");
?>

So, take everything into account now.  You have a function definition with three parameters:

function MysqliCOE($dbname, $DBusername, $DBpassword) {
   /// stuff
}

 

Yet you're trying to invoke it by supplying it only one argument:

MysqliCOE("db_name");

 

You can't do it like that, which is why it's giving you an error.  The error itself tells you that arguments 2 and 3 are missing.  When you invoke a function, generally speaking, you need to pass in the same number of arguments as the definition calls for.

 

In other words, MysqliCOE is expecting you to pass in $dbname, $DBusername, and $DBpassword.  You're only sending it $dbname.  You need:

 

MysqliCOE("db_name", /* something for $DBusername */, /* something for $DBpassword */);

 

Make sense?

So, take everything into account now.  You have a function definition with three parameters:

function MysqliCOE($dbname, $DBusername, $DBpassword) {
   /// stuff
}

 

Yet you're trying to invoke it by supplying it only one argument:

MysqliCOE("db_name");

 

You can't do it like that, which is why it's giving you an error.  The error itself tells you that arguments 2 and 3 are missing.  When you invoke a function, generally speaking, you need to pass in the same number of arguments as the definition calls for.

 

In other words, MysqliCOE is expecting you to pass in $dbname, $DBusername, and $DBpassword.  You're only sending it $dbname.  You need:

 

MysqliCOE("db_name", /* something for $DBusername */, /* something for $DBpassword */);

 

Make sense?

 

Yes, I'm perfectly aware of that. And that's what I'm trying to avoid. I'm going to be coding with a team of coders, and I don't want them to know the MySQL Username or Password. Thus I made this function so they would only need to know the Database name.

 

You're the one that told me to pass the variables in the argument (instead of using global), and I see no way of doing that.

So change the function definition to only accept one parameter:

 

MysqliCOE($dbname) {
   // stuff
}

 

You'll still need to figure out how to get it the username and password.  You have a couple options, like includeing a configuration file with that info, or reading it from a text file with fgets.  Both are better options than passing them in by global.

I'm just hard coding the username and password into each function. It just seems like a waste to include a file into each function every time a new connection needs to be established. If I ever decide to change the username and password I need to update it in 4 functions; but, oh well.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.