Database Connection With Mysql

3,964 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,964
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
0
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Database Connection With Mysql

  1. 1. PHP – Database connection with mysql Harit Kothari [email_address]
  2. 2. Agenda <ul><li>Installation </li></ul><ul><li>service, port, Running through WAMP </li></ul><ul><li>Database creation </li></ul><ul><li>Table creation </li></ul><ul><li>mysqldump and mysqlimport utility </li></ul><ul><li>phpmyadmin utility </li></ul>
  3. 3. Installation <ul><li>Download mysql database installation pack </li></ul><ul><li>Install it by following manual steps </li></ul><ul><li>Start mysql service by using appropriate menu </li></ul>
  4. 4. service, port, Running through WAMP <ul><li>mysql runs as a OS service </li></ul><ul><li>This service provides access to database </li></ul><ul><li>To access, we require 2 parameters: </li></ul><ul><ul><li>Service host name </li></ul></ul><ul><ul><li>Port </li></ul></ul><ul><li>PHP supports many RDBMS </li></ul><ul><li>mysql is most suitable and famous with PHP </li></ul>
  5. 5. Database & Table creation <ul><li>Either use phpmyadmin to create / alter OR </li></ul><ul><li>Go to mysql prompt and execute as under: </li></ul><ul><ul><li>shell>create database db_name </li></ul></ul><ul><ul><li>shell>create table table_name (colname datatype [size] [, ....] constraints...) </li></ul></ul>
  6. 6. Data source names / DSN <ul><li>Syntax: dbsyntax://username:password@protocol+hostspec/database </li></ul><ul><li>Where: </li></ul><ul><ul><li>dbsyntax represents RDBMS name </li></ul></ul><ul><ul><li>Username & password of database user for authentication </li></ul></ul><ul><ul><li>Protocol is optional – e.g. TCP </li></ul></ul><ul><ul><li>Hostspec is hostname on which datbase service is running </li></ul></ul><ul><ul><li>Database is actual database name under dbsyntax </li></ul></ul>
  7. 7. DSN Examples <ul><li>mysql:///webdb </li></ul><ul><li>mysql://localhost/webdb </li></ul><ul><li>mysql://bondview@localhost/webdb </li></ul><ul><li>mysql://bondview@tcp+localhost/webdb </li></ul><ul><li>mysql://bondview:007@localhost/webdb </li></ul>
  8. 8. <ul><li>Syntax DataBase </li></ul><ul><li>Mysql MySQL </li></ul><ul><li>Pgsql PostgreSQL </li></ul><ul><li>Ibase InterBase </li></ul><ul><li>Msql Mini SQL </li></ul><ul><li>Mssql Microsoft SQL Server </li></ul><ul><li>oci8 Oracle 7/8/8i </li></ul><ul><li>Odbc ODBC </li></ul><ul><li>Sybase SyBase </li></ul><ul><li>Ifx Informix </li></ul><ul><li>Fbsql FrontBase </li></ul>
  9. 9. Database connection <ul><li>$db = DB::connect(DSN [, options ]); </li></ul><ul><li>DB::isError($db) This will return if any error occurred while connecting to DataBase with DSN </li></ul><ul><li>Options are: </li></ul><ul><ul><li>persistent - Connection persists between accesses </li></ul></ul><ul><ul><li>optimize - What to optimize for </li></ul></ul><ul><ul><li>debug - Display debugging information </li></ul></ul>
  10. 10. Mysqldump – Dumping DataBase <ul><li>A utility to dump database for backup or offline modification </li></ul><ul><li>A CLI utility that can be executed from SQL prompt to take regular backup of database / table as per requirements </li></ul>
  11. 11. <ul><li>Ways to dump sql DB </li></ul><ul><ul><li>shell> mysqldump [options] db_name [tables] </li></ul></ul><ul><ul><li>shell> mysqldump [options] --databases db_name1 [db_name2 db_name3...] </li></ul></ul><ul><ul><li>shell> mysqldump [options] --all-databases </li></ul></ul>
  12. 12. Play with PHP <ul><li>Connect to database : mysql_connect(&quot;host_name&quot;, &quot;db_username&quot;, &quot;db_password&quot;); </li></ul><ul><li>Select Database : mysql_select_db(&quot;db_name&quot;); </li></ul>
  13. 13. Mysqlimport – Import data <ul><li>Import database, by importing one oe more text files </li></ul><ul><li>Syntax: shell> mysqlimport options db_name input_file </li></ul><ul><li>Useful especially while importing data from CSV files that contains data </li></ul>
  14. 14. phpmyadmin <ul><li>Manage mysql database – an administration tool </li></ul><ul><li>Tool itself written in PHP to manage DataBase effectively and easily </li></ul><ul><li>Supports operations like create, modify, dump and delete database, table, views etc </li></ul><ul><li>Very easy to use, GUI </li></ul>
  15. 15. Summary
  16. 16. Whats next?

×