September 26, 200917 yr Ok so this might seem like an easy task for most php programmers but for beginners who are just starting out might not be able to sucessfully connect to a mysql database so i decided to write this short tutorial on it Mysql_connect what it does Ok so the mysql_connect function is pretty much the bread and butter of connecting to the database so first imma give you 3 simple things to remember when you look at this function. 1.Host 2.User 3.Password Ok so let me explain these 3 things for you real fast Host is normally 1 of 2 things localhost or mysql on my site its mysql if ur using a testing server most always it will be localhost. Ok so lets move onto User this is the username you use to sign into phpmyadmin and password is the password you use to sign into phpmyadmin. so in the end the connect function should look something like this mysql_connect(localhost,user,'pass') or die ("Could not connect to database"); Oh and btw the or die statement is only called if for some reason your site can not connect to the database then it will output Could not connect to database Mysql_select_db Very easy one Ok so this function is basically just the name of your database so heres the example mysql_select_db(mydatabase); Lets put it all together $host = "localhost"; $user = "user"; $pass = "pass"; $db = "database name"; mysql_connect($host,$user,$pass) or die ("Could not connect to the database"); mysql_select_db($db); Ok so thanks for reading and i hope beginners in php have took away something useful from this tutorial
Create an account or sign in to comment