Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

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
PLUS
Rasbin Rijal
Courses Plus Student 10,864 Points

Hi jayk jayk ,

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

Example :

<?php

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