Contents • Localhost • PhPMyAdmin/ MySQL Admin • Introduction to PhP • Using HTML forms in PhP • Connecting to MySQL database • Retrieving data from MySQL database • Adding data to MySQL database Localhost • A localhost is a Web server and associated database that is set up directly on your computer or that may be on a local network either within your home or business environment. • You have direct easy access to the machine upon which the software is situated • A remote host is one that you access via the internet it may be owned by you or you may rent it from a dedicated hosting company. Installation of XAMPP • Go to https://www.apachefriends.org/download.htm l and download latest XAMPP and follow instructions • You can also get a copy from a friend once it is downloaded • You also need text editor eg geany and download from https://www.geany.org/ Localhost directory Localhost • You need to create a directory eg example within htdocs and then save your html or php file into that directory • Then you can access those files by issuing the following commands • http://localhost/example • Remote computers will be able to access these files using the following commands • http://domain/example PhPMyAdmin/ MySQL Admin • You can access XAMPP from the web browser by the following command • http://localhost/xampp • You can access phpmyadmin from the web broswer by the following command • http://localhost/phpmyadmin • If you have setup the password, then you will be requested to enter the user name and password PhPMyAdmin/ MySQL Admin • You can use phpmyadmin to do the following: – Create database and tables easily using the GUI – Query the database by sending SQL commands using the GUI – Create PHP code from SQL commands – Import and export data from other data sources – Administer MySQL database and grant privileges – And much more PhPMyAdmin/ MySQL Admin PhPMyAdmin/ MySQL Admin • To create an sql dump file or create backup (table structure and data) you select export from PhPMyAdmin and select save as sql file • To export to excel you can select excel csv (comma delimited file) • You can also use mysqldump command. Read more on this command from the manual • At command prompt you can issue the following command mysqldump databasename --user root –password = rootpassword > filename.sql Introduction to PhP • PHP is scripting language that can help you to retrieve data from MySQL and print them on the web browser • It uses HTML commands with web programming functionality • PHP scripts are saved with the file extension .php to signify their type. Whenever your web server is asked to send a file ending with .php, it first passes it to the PHP interpreter, which executes any PHP code in the script before returning a generated file to the end user. • The basic unit of PHP code is called a statement, and ends with a semicolon to signify it is a complete statement. • You take your own time to learn PHP (Internet/Intranet can help) Introduction to PhP • Here how to create your first PHP code • Create folder example in the htdocs • Create a text file called hello.php and enter the following text <?php echo "Hello, world!"; ?>
• Go to your browser and enter the following URL
http://localhost/example/hello.php Using HTML forms in PhP • You can use html input forms in the php
<form action="someform.php" method="post">
Enter Name: <input type="text" name="Name" value=“Kunda" /><br /> Enter Password: <input type="password" name="Password" /><br /> Age: <input type="text" name="Age" /><br /> <input type="submit" /> </form> Connecting to MySQL database • Connecting to database $conn = new mysqli($server,$username,$password,$database); • mysqli() requires four parameters • Server name, this is the URL of the MySQL server you want to connect to eg localhost • User name, this username in MySQL • Password, this is password associated with the MySQL user identified by user name • Database, this is name of the database that you wish to connect to $conn = new PDO("mysql:host=$server;database=$dbname","$username ","$password"); Creating a Query • Creating a query is very easy now that you are connected and selected the database $sql = "SELECT * FROM staff"; $result = $conn->query($sql); Retrieving data from MySQL database • There are many ways to extract data from the $result variable, one way is treat them as an associative array while ($row = $result->fetch_assoc()){ foreach ($row as $col=>$val){ print “$col: $val <br>”; } // end foreach print “<br>”; } // end while Formatting data print "<table border = '1'>\n"; print "<tr>\n"; while ($field = mysqli_fetch_field($result)){ print " <th>$field->name</th>\n"; } // end while print "</tr>\n\n"; while ($row = $result->fetch_assoc()){ print "<tr>\n"; foreach ($row as $col=>$val){ print “<td>$val</td>\n"; } // end foreach print "</tr>\n\n"; }// end while print “</table>”; Adding data to MySQL database <form action = “firstform.php“ method = "post"> <fieldset> <label>Enter Staff No.</label> <input name = “staffno“ value = "" /> <br> <label>Enter Staff Name</label> <input name = “lname” value = "" /> <br> <label>Enter Staff Salary</label> <input name = “salary” value = "" /> <br> <button type = "submit"> Submit </button> </fieldset> </form> Adding data to MySQL database // obtain the data from input variables $staffno = $dbConn -> escape_string($_POST['staffno']); $lname = $dbConn -> escape_string($_POST['lname']); $salary = $dbConn -> escape_string($_POST['salary']); // connect to mysql and select database $dbConn = new mysqli($hostname,$username,$password,$database); Adding data to MySQL database $query = “INSERT INTO staff VALUES ('$staffno', '$lname', '$salary' "; $result = $conn->query($query); if ($result){ print "<h1>data added successful</h1>\n"; } else { print "<h2>there was a problem...</h2>$query\n"; } // end if Edit and delete records • You can use the same concept of connecting to database and then select a delete query to delete a record from a table • You can use the concept of add data to database to retrieve data first and then edit it in the form and send back to the database CRUD Example • Download the zipped file and extract it into right folder • Using phpMyAdmin create a table CREATE TABLE `products` ( `id` int(10) NOT NULL AUTO_INCREMENT, `productname` varchar(40) NOT NULL, `quantity` int(10) NOT NULL, `price` double NOT NULL, PRIMARY KEY (`id` ) ); • Insert some data INSERT INTO `products` (`id`, `productname`, `quantity`, `price`) VALUES (1, 'Mangoes', 200, 450), (2, 'Apples', 10, 240); • Using browser enter the following URL http://localhost/ict461/phpcrud Lab Exercise • Remember to apply the concept of CRUD in your lab/ course work