Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to start a PHP/Mysql connection?

simple question to know how to start using php to connect to db in mysql

user-image
Question added by Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company
Date Posted: 2013/08/18
Usama Saad
by Usama Saad , Senior Software Engineer (Java) , Saudi Catering Company

here is a simple connection between php and mysql. in5 steps .

Step1: create the db

CREATE DATABASE `mydb`;USE `mydb`;CREATE TABLE `employee` ( `id` int UNIQUE NOT NULL AUTO_INCREMENT, `first_name` varchar(40), `last_name` varchar(50), PRIMARY KEY(id));INSERT INTO cars VALUES('usama','saad');INSERT INTO cars VALUES('hasan','hamaky');INSERT INTO cars VALUES('noor','sobhy');

Step2: Get Connection with the db

$username = "your_name";$password = "your_password";$hostname = "localhost"; //connection to the database$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");echo "Connected to MySQL ";?>

 

Step3 Get your target schema

//select a database to work with$selected = mysql_select_db("mydb",$dbhandle) or die("Could not select mydb");?>

 

Step4: Start your interaction with the db:

//execute the SQL query and return records$result = mysql_query("SELECT id, first_name, last_name FROM employee");//fetch tha data from the databasewhile ($row = mysql_fetch_array($result)) { echo "ID:".$row{'id'}." Name:".$row{'first_name'}." ".$row{'last_name'}." "; }?>

 

Step5: Close the connection: //close the connectionmysql_close($dbhandle);?>

refrence :

http://usama-saad.blogspot.com/2013/08/how-to-start-php-mysql-connection.html

Simple! You can make a connection to your MySQL database from PHP using the mysql_connect() function. Here is its example:

 

<?php

mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;

mysql_select_db("Database_Name") or die(mysql_error()) ;

?>

The first variable (shown as your.hostaddress.com) is the location of your database. Often this is "localhost". The next two variables are your username and password. This is the login you use for your MySQL database.

In the second line, we choose which database you are connecting too. This is needed because the same MySQL username can be linked to several databases.

Cheers!

Muhammad Majid Saleem
by Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

You may read here about it http://pk1.php.net/function.mysql-connect

محمد عبد المجيد محمد
by محمد عبد المجيد محمد , Senior Web Developer Leader , شبكات لتكونولوجيا المعلومات

I recommended using MYSQLI extention because mysql is Deprecated in php5 $_link_ = mysqli_connect( host , user , pass , database name ) or die( 'Mysql Error;'.mysqli_error()) ;

Shabeer M Moodambath
by Shabeer M Moodambath , web designer / developer , Coolblack

<?php

mysql_connect("hostName","dbUserName","dbPassword");

?>

lenin manoharan
by lenin manoharan , Quality Supervisior , datafield india pvt limited,coimbatore

step:1

To connect Database using this command

<?php

$connect=mysql_connect("localhost","root","") or die("Not connect to server");

$select=mysql_select_db("db_name",$connect) or die("Not connect with your DB");

?>

Ishagh Bah
by Ishagh Bah , Developer and system expert , Orbit

Hi

1- you should have the mysql module a .dll in windows and .so in linux (look in module directory)

2- it should be enabled in php.ini file

3-call function mysql_connect

Mistry Biren
by Mistry Biren , In-Charge Stores & EDP , POLYMECHPLAST MACHINES LTD.

it is very simple create php variable

step-1

$link = mysql_connect("localhost","root","123456") or die('Faile to login');

1. localhost is your local web server

2. root or any user you have created is your username with permited previlage

3 "123456" password given by the mysql user

step-2

mysql_select_db("pml",$link) or die("not connected")

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.