Contact Support : +64 220 256 070


How to connect to Mysql using PHP?

Posted on: August 28, 2014

This lesson will teach on how to establish Mysql connection using PHP script. You would need to have two important things for achieving it.

1) Mysql_connect() function which is also known as Mysqli_construct(Mysqli_connect).

2) Mysql server

Following is a simple script for connecting PHP to the Mysql server using the extension mysqli. 

$link mysqli_connect(“myhost”,“myuser”,“mypassw”,“mybd”) or die(“Error ” mysqli_error($link));

$query “SELECT name FROM mytable” or die(“Error in the consult..” mysqli_error($link));

//execute the query.

$result mysqli_query($link$query);

//display information:

while($row mysqli_fetch_array($result)) {
$row[“name”] . “<br>”;


While including this in your PHP script, define ‘myhost’ as the server hostname or the IP address. The parameters ‘myuser’ and ‘mypassw’ are Mysql user and it’s password for the database. The database name is represented as ‘mybd’.

Now you can query the database using the PHP script, you can check it by accessing the URL for the script from a browser.



Search Blog