WTA9 PHP Database Connectivity

WTA9 PHP Database Connectivity - 1 PHP DataBase...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 PHP DataBase Connectivity • MySQL • SimpeXML 1 PHP & MySQL 2 2 PHP API b All interaction between PHP and the MySQL database engine is through the PHP-MySQL extension, a comprehensive set of built-in PHP functions for accessing MySQL. b All objects and resources under MySQL are created, accessed and manipulated via SQL statements. These SQL statements get processed and executed by the MySQL Database Engine. 3 Connecting to MySQL Db Engine b Syntax: $dbcon=@mysql_connect(localhost, $username, $password); b This function is used to establish a connection to the MySQL Db engine running in memory on the local computer or an entirely different computer. b This function returns a number that identifies a connection that has been successfully established. 4 3 Selecting the Database b After connecting to the MySQL Db engine, the database has to be selected with username & access permissions. b Syntax : mysql_select_db($database_name,$dbcon); b Once the connection is successful it is possible to access data stored in the database tables. 5 Executing Commands mysql_query($query); b Where $query is a variable initialised to any SQL query like CREATE, INSERT, UPDATE, DELETE, SELECT. mysql_numrows() b Number os rows that have been selected from a table. mysql_affected_rows() b To determine how many rows were affected by the INSERT, UPDATE or DELETE query. mysql_close() b To close the Db connection. 6 4 Ex 1 : Creating a table <?php /* --------PHP variables bound to Database Connection. ------------------------------ */ $user = "root"; // A variable holding the user name of MySQL database $pwd = “"; // A variable holding the password of MySQL database $db = “test"; // A variable holding the database named bookdb $host = "localhost"; // A variable holding the host name /* Connecting to the database */ @mysql_connect($host, $user, $pwd) or die("Connection Failed"); /* Selecting the database */ mysql_select_db($db) or die("Could not select the database!"); echo “MySQL Database Connection Established”; /* Creating a new table */ $query=“CREATE TABLE AuthorMaster( AuthorID INTEGER(3), AuthorName VARCHAR(25), AuthorDegree VARCHAR(15), Speciality VARCHAR(25), DOB VARCHAR(15))”; if(@mysql_query($query)) { echo(“<BR> Table created successfully.”); } else { echo(“<BR> Error creating table.”);} ?> 7 Ex 2 : Inserting record into a table <?php /* --------PHP variables bound to Database Connection. ------------------------------ */ $user = "root"; // A variable holding the user name of MySQL database $pwd = “"; // A variable holding the password of MySQL database $db = “test"; // A variable holding the database named bookdb $host = "localhost"; // A variable holding the host name /* Connecting to the database */ @mysql_connect($host, $user, $pwd) or die("Connection Failed"); /* Selecting the database */ mysql_select_db($db) or die("Could not select the database!"); echo “MySQL Database Connection Established”; /* Inserting a record */ $query=“INSERT INTO AuthorMaster(...
View Full Document

Page1 / 13

WTA9 PHP Database Connectivity - 1 PHP DataBase...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online