How to connect to mysql database using command line client

How to Connect to a Mysql Database Using Php and Command Line

Two ways to connect to a mysql Database.

Method 1.One can connect to a mysql database using the command line client .Below is the syntax for connecting to using command line client .

How to connect to mysql database using command line client
How to connect to mysql database using command line client

Method 2:The other method of connecting to a mysql database is the use of a server side scripting language like php or perl

I will demonstrate using


<?php
// Connects to your Database
if(!defined('INCLUDE_CHECK')) die("not allowed to execute directly");

$host="127.0.0.1";

$username="root";
$password=""; // Mysql password
$db="essaybay";

 

$link=mysql_connect($host,$username,$password) or die(mysql_error());

mysql_select_db($db,$link);
mysql_query("SET names UTF8");

?>

Note:Incase the mysql database is installed on the same computer that you are running the code for exampe if you are using a wamp server, you can either use 127.0.0.1 or localhost  as the values for host.Replace the root with the database username and password with your password.Then select the database.

My next post is about selecting and displaying data retrieved from a DB  SELECT command.Check it here…

How to Select and Display mysql table data using Select Query

How to Select and Display mysql table data using Select Query

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com