{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

WTA9 PHP Database Connectivity

WTA9 PHP Database Connectivity - PHP DataBase Connectivity...

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

View Full Document Right Arrow Icon
1 PHP DataBase Connectivity MySQL SimpeXML 1 PHP & MySQL 2
Image of page 1

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

View Full Document Right Arrow Icon
2 PHP API box4 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. box4 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 box4 Syntax: [email protected]_connect(localhost, $username, $password); box4 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. box4 This function returns a number that identifies a connection that has been successfully established. 4
Image of page 2
3 Selecting the Database box4 After connecting to the MySQL Db engine, the database has to be selected with username & access permissions. box4 Syntax : mysql_select_db($database_name,$dbcon); box4 Once the connection is successful it is possible to access data stored in the database tables. 5 Executing Commands mysql_query($query); box4 Where $query is a variable initialised to any SQL query like CREATE, INSERT, UPDATE, DELETE, SELECT. mysql_numrows() box4 Number os rows that have been selected from a table. mysql_affected_rows() box4 To determine how many rows were affected by the INSERT, UPDATE or DELETE query. mysql_close() box4 To close the Db connection. 6
Image of page 3

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

View Full Document Right Arrow Icon
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( AuthorID, AuthorName, AuthorDegree, Speciality, DOB) VALUES (1,’Alex’,’M.Tech’,’Open
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern