Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


how to connect php content to my sql database

i want to connect php content to store and retrive data and information

2 Answers

Rasbin Rijal
Rasbin Rijal
Courses Plus Student 10,863 Points

Hi jayk jayk ,

To connect PHP to mysql database you should use mysql_connect() function.

Example :


mysql_connect('localhost','root','') or die(mysql_error());
echo "found server<br>";

mysql_select_db('mydb') or die(mysql_error());
echo "found database pos <br>";


In the above code, the function mysql_connect( ) is used to make a connection to the mysql server. And the function mysql_select_db( ) is used to select the database. Here 'localhost' is my server, 'root' is my user and the third field ' ' is password field which I have left blank. In mysql_select_db( ) function, 'mydb' is the name of my database. You should keep these fields according to your servername, user, password and database name.

The echo lines I have used above is just to test / show a message after the code executes. There are also other methods to do this and it is one of the way I know. Hope this helps. Happy Learning. :)

thanks rijal