Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

1

F5224 WEB PROGRAMMING

PHP and MySQL

ESTABLISH CONNECTION
The implementation of a database aids you to keep or change information entered by your users. In order to perform MySQL functions, MySQL must be installed on your server or on your local computer.

The knowledge of MySQL is necessary before continuing on with PHP MySQL functions.
Before entering, extracting or updating any user input to our MySQL database, we must connect to the MySQL database. Use the following command: mysql_connect()

ESTABLISH CONNECTION
An example: $hostname = "localhost:3306"; $username = "yourusername"; $password = "yourpassword"; mysql_connect($hostname, $username, $password);

As you can see, in order to connect to MySQL database we must know the hostname, username and password of the database. The next function closes the last opened database: mysql_close()
Now let's create our first table in our database We'll call it 'mytable' and store the names and e-mails of our website visitors there) using the following complete PHP script.

ESTABLISH CONNECTION
There are several new MySQL functions in the below script: mysql_select_db("our database name") this function selects our database on the server. mysql_query("query") this function sends a query to the MySQL database. We specified our query in the $query variable.

ESTABLISH CONNECTION
<?php $dbname = "databasename"; $tablename = "mytable"; mysql_connect("$hostname", "$username", "$password") or die("Unable to connect to database");

@mysql_select_db("$dbname") or die("Unable to select database");


$query = "CREATE TABLE $tablename (name VARCHAR(25), email VARCHAR(25))"; $result = mysql_query($query); print "Table created<BR>"; mysql_close(); ?>

DATA MANIPULATION
Well, now that our database table "mytable" is created, let's learn how to input, extract or update the data. Let's assume we have the following HTML form:
<html> <head> <title>HTML form</title> </head> <body> <form method="POST" action="mysql.php"> <p>Your name: <input type="text" name="name" size="20"></p> <p>Your e-mail: <input type="text" name="email" size="20"></p> <p><input type="submit" value="Go!"></p> </form> </body> </html>

DATA MANIPULATION
You can save it as form.html. The ACTION of the above form points to mysql.php file that will contain the PHP script. When the visitor types in their name and e-mail within the HTML form, and presses the "Go!" button, mysql.php file will be called. Let's suppose we want to insert the user's information into the MySQL database table "mytable" from the previous section. The mysql.php would be:

DATA MANIPULATION
<?php

$dbname = "databasename"; $tablename = "mytable";


mysql_connect("$hostname", "$username", "$password") or die("Unable to connect to database"); @mysql_select_db("$dbname") or die("Unable to select database"); $query = "INSERT INTO $tablename VALUES ('$name', '$email')"; $result = mysql_query($query); print "Dear $name. Your name and e-mail ($email) has been inserted into our database."; mysql_close(); ?>

DATA MANIPULATION
Well, now let's find out how to extract the data from the MySQL database. If we have a considerable amount of information and want to print out the data of all Johns:
$query = "SELECT * FROM $tablename WHERE name = 'John'"; result = mysql_query($query); $num = mysql_num_rows($result); $i = 0; print "Data that matched your query: $num<BR>"; while($i < $num) { $name = mysql_result($result,$i,"name"); $email = mysql_result($result,$i,"email"); print "The e-mail of $name is: $email.<BR>"; $i++; }

10

DATA MANIPULATION
As you can see we have two new MySQL functions here: mysql_num_rows() this function gets number of rows in result ($result). mysql_result() this function returns the contents of one database cell. Recommended high-performance alternatives for SELECT statements: mysql_fetch_row() mysql_fetch_array()

You can also easily update or delete the data in your database using mysql_query() function

11

DATA MANIPULATION
For example: $query = "UPDATE $tablename SET name = "Smith" WHERE name = "John"; $result = mysql_query($query); or if you want to delete any of the data: $query = "DELETE FROM $tablename WHERE name = "John"; $result = mysql_query($query);

You might also like