The document provides an overview of using MySQL with PHP, including downloading and installing MySQL, creating databases and tables, adding and querying data, connecting to databases from PHP, and inserting, selecting, and displaying data from PHP.
Overview Download MYSQLGUI What is a database, creating data Creating a database in MySQL SQL scripts Connecting to a database in PHP Viewing records
2.
Download and installmysql gui Download from here http://dev.mysql.com/downloads/gui-tools/5.0.html
3.
What is adatabase About.com http://databases.about.com/od/specificproducts/a/whatisadatabase.htm Wikipedia http://en.wikipedia.org/wiki/Database Tutorial on a database http://www.htmlgoodies.com/beyond/reference/article.php/3472691 Mysql tutorial http://www.w3schools.com/php/php_mysql_intro.asp
4.
What is adata kildare Male Couper Tristan 01/01/1987 kildare Male Connolly Aidan kildare Male Clarke Richard 13/07/1980 meath Female Griffin Sheila 11/11/1977 meath Male Meagher David 01/05/1978 dublin Male O'Sullivan John 31/05/1972 dublin Male Murphy John date of birth County Gender Surname First Name
5.
What is adatabase Address table kildare Male Couper Tristan 7 01/01/1987 kildare Male Connolly Aidan 6 kildare Male Clarke Richard 5 13/07/1980 meath Female Griffin Sheila 4 11/11/1977 meath Male Meagher David 3 01/05/1978 dublin Male O'Sullivan John 2 31/05/1972 dublin Male Murphy John 1 date of birth County Gender Surname First Name id
6.
What are datatypes Address table kildare Male Couper Tristan 7 01/01/1987 kildare Male Connolly Aidan 6 kildare Male Clarke Richard 5 13/07/1980 meath Female Griffin Sheila 4 11/11/1977 meath Male Meagher David 3 01/05/1978 dublin Male O'Sullivan John 2 31/05/1972 dublin Male Murphy John 1 date varchar varchar varchar varchar integer
7.
What is datanormalization Address table kildare Male Couper Tristan 7 01/01/1987 kildare Male Connolly Aidan 6 kildare Male Clarke Richard 5 13/07/1980 meath Female Griffin Sheila 4 11/11/1977 meath Male Meagher David 3 01/05/1978 dublin Male O'Sullivan John 2 31/05/1972 dublin Male Murphy John 1 date of birth County Gender Surname First Name id
8.
What is datanormalization Address table county table kildare Male Couper Tristan 7 01/01/1987 kildare Male Connolly Aidan 6 kildare Male Clarke Richard 5 13/07/1980 meath Female Griffin Sheila 4 11/11/1977 meath Male Meagher David 3 01/05/1978 dublin Male O'Sullivan John 2 31/05/1972 dublin Male Murphy John 1 date of birth County Gender Surname First Name id meath kildare dublin County
9.
What is datanormalization Address table county table 2 Male Couper Tristan 7 01/01/1987 2 Male Connolly Aidan 6 2 Male Clarke Richard 5 13/07/1980 3 Female Griffin Sheila 4 11/11/1977 3 Male Meagher David 3 01/05/1978 1 Male O'Sullivan John 2 31/05/1972 1 Male Murphy John 1 date of birth CId Gender Surname First Name id meath 3 kildare 2 dublin 1 County Cid
10.
Connecting to MySQLLoad up MYSQL ADMIN Server is localhost Username is root Port is 3306
11.
Creating a databasein MySQL Click on catalogues in the list And right click inside schemas to create a new database
12.
Creating a tablein MySQL Create a new table called contacts with the following information ID is always a primary key Should be set to autoincrement
13.
Adding data inMySQL Right click the new table and choose EDIT TABLE DATA, this opens the table editor
14.
Adding data inMySQL To enter data click the edit button and enter details into the fields. The ID key should autoincrement
15.
MySQL Query toselect data SELECT * FROM contacts; SELECT CFName FROM contacts; SELECT * FROM contacts WHERE id = 4; SELECT * FROM contacts ORDER BY CFName; Operator Description = Equal != Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern – use % for wild card characters - use single quotes for strings, no quotes for numbers
16.
MySQL Query toinsert data INSERT INTO contacts (CFName, CSName, CAge) values ('Big' ,'tom', 23);
17.
MySQL Query toupdate data The SQL statement UPDATE is used to change the value in a table. For example to change CFName from big to small UPDATE contacts SET CFName = 'small' WHERE CFName = 'big'; Note: SQL statements are not case sensitive, e.g. WHERE is the same as where .
18.
MySQL Query toDelete data The DELETE statement deletes rows from a table that satisfy the condition given by the WHERE clause. For example to delete a record from contact table where id equals 1 DELETE FROM contacts WHERE CId = 1;
19.
Connecting to aDatabase through PHP Opening a connection to MySQL from PHP is easy Just use the mysql_connect() function like this mysql_connect(servername,username,password);
20.
Connecting to aDatabase through PHP <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql:'. mysql_error()); $dbname = ' dynamicweb '; mysql_select_db($dbname); ?>
21.
Connecting to aDatabase through PHP It's a common practice to place the routine of opening a database connection in a separate file. Then every time you want to open a connection just include the file. Usually the host, user, password and database name are also separated in a configuration file. <?php // This is an example of dbconfig.php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $dbname = 'myDBname'; ?> <?php // This is an example dbopen.php $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?>
22.
Connecting to aDatabase through PHP <?php include 'DBconfig.php'; include 'DBopen.php'; // ... do something like insert or select, etc ?>
23.
Closing a Databasethrough PHP <?php // an example of closedb.php // it does nothing but closing // a mysql database connection mysql_close($conn); ?> <?php include 'config.php'; include 'opendb.php'; // ... do something like // insert or select, etc include 'dbclose.php'; ?>
24.
Listing data ina database using PHP To get PHP to execute MySQL statements we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. e.g.: Using PHP you can run a MySQL SELECT query to fetch the data out of the database. You have several options in fetching information from MySQL. PHP provide several functions for this. The first one is mysql_fetch_array() which fetches a result row as an associative array , a numeric array , or both. Below is an example of fetching data from MySQL, the table contacts has three columns; cF name , c Lname and c Age .
25.
Listing data ina database using PHP <?php include 'DBconfig.php'; include 'DBopen.php'; $query = "SELECT cFname, cLname, cAge FROM contacts"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "First Name: $row[Fname] <br>". "Second Name: $row[Lname] <br>". "Age: $row[age] <br><br>"; } include 'DBclose.php'; ?>
26.
Insert Data froma Form into a Database <html> <body> <form action=" insert.php " method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
27.
Insert Data froma Form into a Database <?php //php file “ insert.php ” $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="INSERT INTO table_name (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?>
28.
Select Data Froma Database Table with php <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM table_name "); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } mysql_close($con); ?>