Connecting to mysql Database in php three variables need to be specified.These are mysql server address- normally,when connecting to a m...
Connecting to mysql Database in php three variables need to be specified.These are
Once you have connected to the Mysql server,you need to select the database to use and set mysql_query as UTF8.
Its advisable to save the connection code to the server as a separate file to avoid repeating the same code whenever you want to connect to the database in different pages. i.e
Save the connection file as connect.php
and whenever you want to connect to the database,just include the file in start of the next page using require() function
- mysql server address-normally,when connecting to a mysql database hosted locally you should use localhost or 127.0.0.1 as the server address.
- username-mysql server has a table called users which stores passwords and username for people allowed to connect to the database.
- password-every user connecting to mysql database needs to have a password.
Below is example of a code that connects to a mysql server hosted locally.
<?php $host="localhost";
// Mysql Server Host name/address
$username="root";
// Mysql username root
$password="";
// using no password
$link=mysql_connect($host,$username,$password) or die(mysql_error());
//assign the connection a variable //once the connection is successful...select the database to use.
$db="download"; mysql_select_db($db,$link); mysql_query("SET names UTF8"); ?>
Once you have connected to the Mysql server,you need to select the database to use and set mysql_query as UTF8.
Its advisable to save the connection code to the server as a separate file to avoid repeating the same code whenever you want to connect to the database in different pages. i.e
Save the connection file as connect.php
and whenever you want to connect to the database,just include the file in start of the next page using require() function
require 'connect.php';