Noximity Posted December 20, 2016 Share Posted December 20, 2016 Hello users on phpfreaks! I have recently started my journey to master PHP coding. I have started creating my own website that uses php but I have been receiving one error that I can't quite fix "( ! ) Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp64\www\link.php on line 4" I have been trying to fix this for quite a while now and I can't seem to get my link.php to work properly! If anyone can give me a fix it would be highly appreciated! My code will be shown below! <?php @include_once 'settings.php'; //session_start $link = mysqli_connect($servername, $username, $password); //MySQL Host, Username, password $db_selected = mysqli_select_db('database', $link); //MySql database mysqli_query("SET NAMES utf8"); function fetchinfo($rowname, $tablename, $finder, $findervalue) { if ($finder == "1") $result = mysqli_query("SELECT $rowname FROM $tablename"); else $result = mysqli_query("SELECT $rowname FROM $tablename WHERE `$finder` =`$findervalue`"); $row = mysqli_fetch_assoc($result); return $row[$rowname]; } function secureoutput($string) { $string = mysqli_real_escape_string($string); $string = htmlentities(strip_tags($string)); $string = str_replace('>', '', $string); $string = str_replace('<', '', $string); $string = htmlspecialchars($string); return $string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/ Share on other sites More sharing options...
ginerjm Posted December 20, 2016 Share Posted December 20, 2016 Have you tried using the PHP manual as part of your 'mastering' experience? It would be helpful in showing you the proper syntax for the functions you want to use. bool mysqli_select_db ( mysqli $link , string $dbname ) Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540585 Share on other sites More sharing options...
mac_gyver Posted December 20, 2016 Share Posted December 20, 2016 (edited) it's not possible to write code or convert old code you have found on the web, without using the php.net documentation. next, the php mysqli extension is not the best choice to use. learn and use the php PDO extension instead. you should be using prepared queries to supply data to any sql query statement to protect against sql injection. don't use @ error suppressors, ever. all they do is hide problems and make it harder to write code that works. don't write and use functions like the fetchinfo() function. it is currently not secure and by having a function that selects a single column at a time, you will end up killing your database server with too many queries. don't write and use functions like the secureoutput() function. this function is repeating what it is doing, using a msyqli function, which has nothing to do with output, and is just nonsense code. to secure data being output to a web page, just use htmlentities() with the appropriate flag values. Edited December 20, 2016 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540586 Share on other sites More sharing options...
cyberRobot Posted December 20, 2016 Share Posted December 20, 2016 (edited) You should use mysqli_connect() to select the database. More information can be found here: http://php.net/manual/en/function.mysqli-connect.php Note: the documentation recommends that you only use mysqli_select_db() to change the default database. More information can be found here: http://php.net/manual/en/mysqli.select-db.php Edited December 20, 2016 by cyberRobot removed the ginerjm quote since it was misleading and unnecessary...and I wanted to increase the confusion :-) Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540597 Share on other sites More sharing options...
ginerjm Posted December 20, 2016 Share Posted December 20, 2016 I think you have mis-quoted me. My post was to show the OP what the manual shows as the proper syntax and nothing more. Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540598 Share on other sites More sharing options...
benanamen Posted December 20, 2016 Share Posted December 20, 2016 @cyberRobot, the OP has mysqli_select_db () bass akwards. Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540602 Share on other sites More sharing options...
Barand Posted December 20, 2016 Share Posted December 20, 2016 ... the OP has mysqli_select_db () bass akwards. I remember telling one guy on the forum that he had two parameters in the wrong order. His reply was "What order should they be then?" 3 Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540605 Share on other sites More sharing options...
cyberRobot Posted December 20, 2016 Share Posted December 20, 2016 (edited) I think you have mis-quoted me. My post was to show the OP what the manual shows as the proper syntax and nothing more. Yeah, sorry about that. I only "quoted" you to refer to the unnecessary function. As soon as I hit post, I realized that wasn't the best way...but was in too much of a rush to correct it. I figured it would be better to ask for forgiveness. Edit: I removed the quote since it was misleading and unnecessary. @cyberRobot, the OP has mysqli_select_db () bass akwards. Yep Edited December 20, 2016 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/302788-mysqli-expects-parameter-1-to-be-mysqli/#findComment-1540608 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.