Basic tips for coonect to mysql with php

<?php
mysql_connect(“localhost”, “admin”, “1admin”) or die(mysql_error());
echo “Connected to MySQL<br />”;
?>

If you load the above PHP script to your webserver and everything works properly, then you should see “Connected to MySQL” displayed when you view the .php page.

The mysql_connect function takes three arguments. Server, username, and password. In our example above these arguments were:

  • Server – localhost
  • Username – admin
  • Password – admin

Now let’s say you want to access a specific database you can use:

After establishing a MySQL connection with the code above, you then need to choose which database you will be using with this connection. This is done with the mysql_select_db function.

<?php
mysql_connect(“localhost”, “admin”, “1admin”) or die(mysql_error());
echo “Connected to MySQL<br />”;
mysql_select_db(“test”) or die(mysql_error());
echo “Connected to Database”;
?>
the display message should be:

Connected to MySQL
Connected to Database

this is a quick little tutorial on how to conect to a MySql databse, the next tutorila will elaborate on how to create tables in that database and all the diffrent operations that you can perform.

Leave a Reply