Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

Connecting to a MySQL Database

In order to pull information from a MySQL database to your php pages, you need to connect to the database first.
Syntax:
mysql_connect(servername,username,password);
servername: It specifies the server to connect to. Default value is "localhost:3306"
username: It specifies the username to log in with. Default value is the name of the user that owns the server process
password: It specifies the password to login. Default is ""

Note:
To execute the SQL statements we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.


Example:
$server = "localhost"; 
$user = ""; 
$pass = ""; 
$db = "mydb"; 

$conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect.");
mysql_select_db("$db", $conn);

?>

Create a Database

PHP also provide a function to create MySQL database, mysql_create_db().

$con = mysql_connect("localhost:3306","adam","123456");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$query  = "CREATE DATABASE mydb";
$result = mysql_query($query,$con);
?>

Create a Table

CREATE TABLE statement is used to create a database table in MySQL database.

$con = mysql_connect("localhost:3306","adam","123456");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("mydb", $con);
mysql_query("CREATE TABLE emp_info(
emp_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
Emp_name VARCHAR(30),
Emp_age INT)")
 or die(mysql_error()); 

echo "Table Created";

?>

 

 
 
     
   
PHP Tutorial
 
  Introduction
Introduction
  Variables
Variables
  Strings
Strings
  Operators
Operators
  If Else Statement
If Else Statement
  Loops
Loops
  Functions
Functions
  Forms
Forms
  Sessions
Sessions
  Cookies
Cookies
  Files
Files
  E-mail
E-mail
  Error Handling
Error Handling
  File Upload
File Upload
  PHP Databases
PHP Databases
  Connecting to a MySQL Database
Connecting to a MySQL Database
  MySQL Data Types
MySQL Data Types
  Inserting Data Into a Table
Inserting Data Into a Table
  Select Statement
Select Statement
  Where Clause
Where Clause
  Order By
Order By
  Update Statement
Update Statement
  Delete From Statement
Delete From Statement
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download List Building For Beginners  Ebook List Building For Beginners
   
Download Trading Fork Extreme Ebook Trading Fork Extreme
   
Download 7 Great Lies of Network Marketing Ebook 7 Great Lies of Network Marketing
   
Download  Killer Patterns Ebook Killer Patterns
   
Download Marketing Rule Book Ebook Marketing Rule Book
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz