Download presentation
Presentation is loading. Please wait.
Published byRodney Hill Modified over 9 years ago
1
www.bzupages.com PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions Connect with MySQL RDBMS – mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName"); Connect with database – mysql_select_db($dbName, $con_var) or die("Unable to select database $dbName");
2
www.bzupages.com Connection Check Code $link = mysql_connect("localhost", “root", ""); if (!$link) { echo "Can't connect to localhost. The error is "; } Note the use of ! to express Boolean "not".
3
www.bzupages.com PHP Data Base Queries: Nearly all table interaction and management is done through queries: Mysql_query($query, $con) issues SQL statements Basic information searches – $SQL = "SELECT FirstName, LastName, DOB, Gender FROM Patients WHERE Gender = '$Gender‘; $Patients = mysql_query($SQL,$link) or die (“can execute query”); Editing, adding, and deleting records and tables – $SQL = "INSERT INTO Patients (FirstName, LastName) VALUES('$firstName', '$lastName')"; $Patients = mysql_query($SQL);
4
www.bzupages.com PHP Database Cleaning up: close the database connection, It requires the connection as an argument, so that it knows which connection to close. – mysql_close($link);
5
www.bzupages.com examining resulting rows mysql_fetch_array(result) returns an array that is the result row, or NULL if it the last result is reached. – Its results in an array that contains the columns requested both by number and by column name: while($columns=mysql_fetch_array($result)) { echo 'name: '.$columns['name']; echo 'first column: ‘.$columns[0]; }
6
www.bzupages.com mysql_num_rows() This command has the syntax mysql_select_db($result) – where the resource result is the result of a query. It returns the number of rows that are in the result. This is useful in announcing the number results before display of results.
7
www.bzupages.com Conf.php (Code Example) <php? // configuration parameters // database configuration $host = “localhost"; $user = “root"; $pass = “"; $db = “db_it"; ?>
8
www.bzupages.com Example Code include("conf.php"); $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); mysql_select_db($db, $connection) or die ("Unable to select database!"); $query = "INSERT INTO papers (title, authors, description, comment, super, bibtex, url, genre) VALUES ('$title', '$authors', '$description', '$comment', '$super','$bibtex','$url','$genre')"; $result = mysql_query($query, $connection) or die ("Error in query: $query. ". mysql_error()); // close database connection mysql_close($connection);
9
www.bzupages.com Example Code include("conf.php"); $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); mysql_select_db($db,$connection) or die ("Unable to select database!"); $query = "INSERT INTO papers (title, authors, description, comment, super, bibtex, url, genre) VALUES ('$title', '$authors', '$description', '$comment', '$super','$bibtex','$url','$genre')"; $result = mysql_query($query, $connection) or die ("Error in query: $query. ". mysql_error()); // close database connection mysql_close($connection);
10
www.bzupages.com Example Code include("conf.php"); $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db,$connection ) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM papers WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. ". mysql_error()); // close database connection mysql_close($connection);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.