HDFilmMaker2112 Posted May 31, 2012 Share Posted May 31, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/ Share on other sites More sharing options...
HDFilmMaker2112 Posted May 31, 2012 Author Share Posted May 31, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350037 Share on other sites More sharing options...
KevinM1 Posted May 31, 2012 Share Posted May 31, 2012 Don't use 'global'. Instead, pass $DBusername and $DBpassword as arguments to your MysqliPersist function. Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350050 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 31, 2012 Author Share Posted May 31, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350149 Share on other sites More sharing options...
KevinM1 Posted May 31, 2012 Share Posted May 31, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350151 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 31, 2012 Author Share Posted May 31, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350163 Share on other sites More sharing options...
KevinM1 Posted May 31, 2012 Share Posted May 31, 2012 Okay, can you show me how you're trying to invoke that function? Maybe lines 50 - 69 in your db_select.php file? Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350164 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 31, 2012 Author Share Posted May 31, 2012 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350166 Share on other sites More sharing options...
KevinM1 Posted May 31, 2012 Share Posted May 31, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350167 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 31, 2012 Author Share Posted May 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350169 Share on other sites More sharing options...
KevinM1 Posted May 31, 2012 Share Posted May 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350170 Share on other sites More sharing options...
HDFilmMaker2112 Posted May 31, 2012 Author Share Posted May 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/263419-help-connecting-to-mysqli/#findComment-1350173 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.