how to establish a connection in php?

December 11, 2007 1:48pm CST
hi everyone ,i am beginner to php.plz anyone help me how to establish a connection with php and mysql?
1 person likes this
3 responses
@swatig (1187)
• India
24 Dec 07
hi preethivarma, You should refer to tutorials first before putting question here. anyways, here is the solution for your problem. ------------------------------------------- -----------------------------------------------
1 person likes this
• India
26 Dec 07
That above one is correct but we can do the same in one sentence like this $con= mysql_connect("localhost","peter","abc123") or die(mysql_error()); Put myswl_error() only for debugging purpose. For a live site show some error like "Database connection failed" within die().
1 person likes this
• Netherlands
19 Jan 08
Let me explain you what Swatig did, and what can be improved. But, in order for that, I need to know. Will you be handling 1 connection to the SQL server? First so, using "strings" in a function that will only be called 1 time can be done better by using variables, like: $server = "localhost"; $username = "insertusernamehere"; $password = "insertpasswordhere"; mysql_connect($server,$username,$password) or die ("Could not connect to the MySQL server, please check the connection details); This way, you don't have to declare a variable. You don't have to go if / else'ing. If you want to use the connection, you can just do mysql_query("SELECT * FROM whatever"); You don't have to use the connection declaration.
• Pakistan
8 Jan 08
Yes you are a beginner. And we say welcome you here at mylot. You are also beginner to php and missal. These are two languages of computer sciences. These does not concern with the establishment of connection. If you want to learn these important languages of computer then you should search the related sites with the help of search engines.
6 Mar 08
i think php can connect mysql by mysql_connect and mysql_pconnect,
@ganeshj86 (256)
• India
1 Oct 09
HI preethivarma, I think you know the server for run the php codes.there are servers like xampp,wamp for run the php programs.for example if you use the xampp server first start the mysql and apache in xampp software.Then create a php program to connect mysql.In notepad you type the following line for connecting. if you already set the user name and password type that in user name and password field.if you don't have type root as user name and blank the password field. then next go to mysql using the following steps go to command prompt type the following c:\cd xampp\mysql\bin type the correct address of where u install the xampp.i installed it in c drive. then type c:\xampp\mysql\binmysql -u root -p then press enter then it you ask you for the password if you have the password type it if you dont have then press enter to connect with mysql.