2. How to start with PHP?
Check the slides of the previous presentation:
http://www.slideshare.net/thingslab/php-programming-intro
If you need help ask to the staff
Summary of some useful unix commands:
sudo apt-get install package-name
sudo apt-cache search php- |more
sudo service apache2 restart
3. Enable the PHP Error Reporting
In the terminal, execute:
cd /etc/php5/apache2 ; sudo leafpad php.ini &
Search and remove the comment instruction ; from
display_errors
Default Value: On
Development Value: On
Production Value: On
error_reporting
Default Value: E_ALL & ~E_NOTICE
Development Value: E_ALL | E_STRICT
Production Value: E_ALL & ~E_DEPRECATED
4. Test a code with errors
Restart apache from command line:
sudo service apache2 restart
Execute the following code:
<?php
print "error in the next line! What is it wrong?"; // after remove
parint "aaa"; // after remove
print phpinfo(); // dump all the info of the Apache and PHP
?>
The error reporting can be very useful for
debugging and for PHP beginners
5. Install PHP My Admin
Install Mysql Server, Mysql Client, PHP Mysql
module and PHP My Admin:
sudo apt-get install mysql-server-5.1 php5-mysql phpmyadmin
Create an user and add specific permissions to
the user, after create a database and add the the
following tables:
users
books
comments
6. Users
Table: Users
Fields:
id as integer, auto increment, primary ke
login as string of 50 characters, index
password as string of 50 characters
role as string of 10 characters
7. Books
Table: Books
Fields:
id as integer, auto increment, primary ke
title as string of 50 characters, index
description as text
pages as integer
Add as many field you like to test the different
type of data
8. Comments
Table: Comments
Fields:
id as integer, auto increment, primary ke
user_id as integer, index
book_id as integer, index
comments as text
Add as many field you like to test the different
type of data
9. Populate the database
Populate the tables in the database with at least
5 records each.
The following conditions should be followed:
● Table users with one user that has id = 10
● Table books with one book that has id = 50
● Table comments with one comment where the book_id = 50
and user_id = 10
We will play in the next slide using some
advanced SQL queries (Join queries).
http://dev.mysql.com/doc/ Use the right version!
11. Open the PHP Manual
Open the main reference for PHP:
sudo apt-get install mysql-server-5.1 php5-mysql phpmyadmin
Search for mysqli section or open:
http://php.net/manual/en/book.mysqli.php
Check in the manual the following commands:
mysqli_connect
$resource->query ($resource is the connection to the database)
mysqli_fetch_array
mysqli_prepare
mysqli_stmt_bind_param
mysqli_stmt_execute
mysqli_stmt_get_result
mysqli_close
12. Search in the database (users)
//connection: adapt the login/password/database name
$link = mysqli_connect("localhost","root","password","test") or
die("Error " . mysqli_error($link));
//consultation:
$query = "SELECT * FROM users WHERE user_id <> 10" or
die("Error in the consult." . mysqli_error($link));
//execute the query.
$result = $link->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["name"] . "<br>";
}
mysqli_close($link);
13. Insert a value in the database
$a = $_GET["a"];
if (isset($a)) {
// is in one line!
$link = mysqli_connect("localhost","root","password","test") or
die("Error " . mysqli_error($link));
print $a;
$query = "insert into users (name, password) values ('$a', 'abc')";
$result = $link->query($query);
mysqli_close($link);
} else {
print "No variable "a" passed to the program <BR>";
print "user the following syntax filename.php?a=200";
}
14. Execute a statement
$query = "INSERT INTO books (login, password) VALUES
(?,?)"; /* Prepare an insert statement */
$stmt = $mysqli->prepare($query);
$stmt->bind_param("sss", $val1, $val2);
$val1 = 'login-1';
$val2 = 'password-1';
$stmt->execute(); /* Execute the statement */
// another statement executed
$val1 = 'login-2'; $val2 = 'password-2'; $stmt->execute();
/* close statement */
$stmt->close();
15. Create a link in HTML
We can link different pages as this example:
<A HREF=”display.php?id=100”>Book title</A>
In PHP can be used as:
$id = 100;
$title = “This is my book”;
print “<A HREF=”display.php?id=$id”>$title</A>”;
16. What's next?
Propose us which subjects you want to explore,
some examples can be :
- Advanced PHP/HTML
- PHP and Javascript
- DEVOPS in practice
- A real software developed in team using githu?
e.g. Smart Farms
17. Exercises
Link the page where you display the results to a
new page were you display the values of a single
record
Display a page with all the comments of one
specific book. The exercise can be performed
using one or two SQL queries, in our case we
want to use a single SQL join (decide which one
you want to use).
18. Exercises
Delete a record in PHP using a parameter from
HTML
Update a table with some parameter received
from the browser (HTML) and test the different
type of data (e.g. integer, string, data, etc.)
Install and use an example of the PDF module of
PHP based on the previous slides (also use the
slides of the previous workshops)