PhpMyAdmin of XAMPP control Panel Start XAMPP control Panel Start Web server(Apache or Tomcat) Start MySQL( database server) Open browser and go to localhost, this will open XAMPP control panel user interface Click on ‘phpMyAdmin’ tab on left hand corner Click ‘New’ from left hand corner-this will bring interface to create new DB Enter the name of DB and click ‘Create’ Steps… Click on DB name you created from left hand corner Enter the name of table and number of columns Click ‘Go’ Enter columns names, data types, attributes(if any), Null/Not Null, Primary Key/not, Auto-increment or Not Then Click ‘Save’ To insert data to table, click ‘insert’ tab Enter value of each column Click ‘Go’ You repeat this multiple times PHP and MySQL let’s create a simple MySQL database table, and then use PHP to connect to the server, retrieve a set of results, and form at them for display on a web page Create Table: CREATE TABLE items ( itemID int(11) NOT NULL auto_increment, itemName varchar(255) NOT NULL default '', itemPrice float NOT NULL default '0', PRIMARY KEY (itemID) ) TYPE=MyISAM; PHP and MySQL Insert data to table: INSERT INTO items VALUES (1, 'Paperweight', '3.99'); INSERT INTO items VALUES (2, 'Key ring', '2.99'); INSERT INTO items VALUES (3, 'Commemorative plate', '14.99'); INSERT INTO items VALUES (4, 'Pencils (set of 4)', '1.99'); INSERT INTO items VALUES (5, 'Coasters (set of 3)', '4.99'); Displaying data from DB example Now, to do the same thing using PHP, create the following PHP script: <html> <head></head> <body> <?php // open connection to MySQL server $connection = mysql_connect('localhost', 'guest', 'pass') or die ('Unable to connect!'); Example Contd… // select database for use mysql_select_db('db2') or die ('Unable to select database!'); // create and execute query $query = 'SELECT * FROM items'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); Example Contd… // check if records were returned if (mysql_num_rows($result) > 0) { // print HTML table echo '<table width=100% cellpadding=10 cellspacing=0 border=1>'; echo'<tr><td><b>ID</b></td><td><b>Name</b></t d><td><b>Price</b></td></tr>'; Example Contd…. // iterate over record set // print each field while($row = mysql_fetch_row($result)) { echo '<tr>'; echo '<td>' . $row[0] . '</td>'; echo '<td>' . $row[1] . '</td>'; echo '<td>' . $row[2] . '</td>'; echo '</tr>'; } echo '</table>'; } Example Contd… else { // print error message echo 'No rows found!'; } // once processing is complete // free result set mysql_free_result($result); // close connection to MySQL server mysql_close($connection); ?> </body> </html> Example explained To begin communication with the MySQL database server, you first need to open a connection to the server. All communication between PHP and the database server takes place through this connection, which is initialized by the mysql_connect() function. The mysql_connect() function requires three parameters: the host name of the MySQL server, and the MySQL username and password required to gain access to it. If the function is able to successfully initialize a connection, it returns a link identifier, which is stored in the variable $connection. This identifier is used throughout the script when communicating with the database. Example Explained Once a connection has been initialized, the next step is to select a database for use (this is equivalent to the SQL USE command) with the mysql_select_db() command, and then send the server a query through the mysql_query() function. Both functions use the last opened connection as their default for all operations. The result set returned by the query is assigned to the variable $result. This result set may contain, depending on your query, zero or more rows or columns of data. The number of rows in the result set is obtained from the mysql_num_rows() function. Assuming one or more rows exist, the mysql_fetch_row() function is used to iterate over the result set and retrieve rows as arrays. Individual field values can then be accessed as array elements. Example Explained Each result set returned by a query occupies some amount of memory. Once you’re done processing it, therefore, it’s a good idea to use the mysql_free_result() function to free up the used memory for other purposes. And, once you’re done querying the database, close the connection with a call to mysql_close(). Persistent Connection The mysql_pconnect() function opens a “persistent” connection to theserver. Such a persistent connection does not automatically end when the script creating it ends; rather, it remains available for use by other scripts requesting an equivalent connection to the MySQL server. Because such “persistent” connections reduce the need for scripts to open a different connection for every request, they are considered more efficient and can produce performance gains in certain situations. Queries That Alter Data Consider the following example, which demonstrates by asking for user input through a form, and then INSERT-ing that data into the database: <html> <head> <basefont face="Arial"> </head> <body> <?php if (!$_POST['submit']) { // form not submitted ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> Item name: <input type="text" name="name"> Item price: <input type="text" name="price"> <input type="submit" name="submit"> </form> Queries That alter Data <?php } else { // get form input // escape input values for greater safety $name = (trim($_POST['name']) == '') ? die ('ERROR: Enter a name') : mysql_escape_string($_POST['name']);
? die ('ERROR: Enter a price') : $_POST['price'];
// open connection
$connection = mysql_connect('localhost', 'guest', 'pass') or die ('Unable to connect!'); Queries that alter data // select database mysql_select_db('db2') or die ('Unable to select database!'); // create query $query = "INSERT INTO items (itemName, itemPrice) VALUES ('$name', '$price')"; // execute query $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // print ID of inserted record echo 'New record inserted with ID ' . mysql_insert_id() . '<br \>'; // print number of rows affected echo mysql_affected_rows() . ' record(s) affected'; // close connection mysql_close($connection); } ?> </body> </html> Queries that alter Data Here, the user is first presented with a form asking for an item and its associated price. Once the form is submitted, the form input is used inside to create an INSERT query, which is then sent to the database with the mysql_query() method. Because mysql_query() returns a Boolean indicating whether the query was successful, it is possible to check whether the INSERT took place and return an appropriate message. Queries that alter data The mysql_escape_string() function escapes special characters (like quotes) in the user input, so it can be safely entered into the database. If the magic_quotes_gpc setting in your PHP configuration file is enabled, you might need to first call stripslashes() on the user input before calling mysql_escape_string(), to avoid characters getting escaped twice. The mysql_insert_id() function returns the ID generated by the previous INSERT query (useful only if the table into which the INSERT occurs contains an AUTO_INCREMENT field). The mysql_affected_rows() function returns the total number of rows affected by the last operation.