• No results found

Lec 22 PHP - V.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 22 PHP - V.pptx"

Copied!
32
0
0

Loading.... (view fulltext now)

Full text

(1)

1

CS1113 Web Programming

Lecture 22

MySQL Basics

(2)

• In order to have really full featured application. We are going to need to

incorporate a database.

• We can store lots of information in

database, search through it. Added it,

updated. Keep it there for long period of time. There are so many benefits of using database.

(3)

CRUD

• CRUD is acronym and it stands for…

CRUD

Create

,

Read

,

Update

,

Delete

(4)

• We create records in the database.

• We read records back in the database.

• We update records in the database.

• We completely delete records in the

(5)

SQL SELECT (Read)

• Format:

• SELECT * FROM table

WHERE column1 = ‘some_text’

(6)

SQL INSERT (Create)

• Format:

• INSERT INTO table (column1, column2,

(7)

SQL UPDATE (Update)

• Format:

• UPDATE table

SET column1 = ‘some_text’

(8)

SQL DELETE (Delete)

• Format:

• DELETE FROM table

(9)

CREATE COMMAND

• CREATE DATABASE widget_corp;

• USE widget_corp;

• CREATE TABLE subjects (

id int(11) NOT NULL auto_increment,

menu_name varchar(30) NOT NULL,

position int(3) NOT NULL,

visible tinyint(1) NOT NULL,

PRIMARY KEY (id)

(10)

INSERT COMMAND

• INSERT INTO subjects (menu_name, position, visible) VALUES (‘About Widget Corp’, 1, 1);

• SELECT * FROM subjects;

• INSERT INTO subjects (menu_name, position, visible) VALUES (‘Products’, 2, 1);

• INSERT INTO subjects (menu_name, position, visible) VALUES (‘Services’, 3, 1);

(11)

SELECT COMMAND

• SELECT * FROM subjects WHERE visible = 1

ORDER BY position ASC/DESC;

• SELECT id, menu_name FROM subjects WHERE visible = 1

(12)

UPDATE COMMAND

• UPDATE subjects

SET visible = 1

(13)

PHPMyAdmin

www.phpmyadmin.net

• It is already included in WAMP/XAMP one package.

(14)

Using PHPMyAdmin

• It enables you to access your MySQL database

through a GUI. You can easily do the following:

• Drop and create databases

• Create, edit and delete tables

• Create, edit and delete fields

• Enter any MySQL statements

• View and Print table structure • Generate PHP code

(15)
(16)

PHP Database Interaction in

FIVE steps

1. Create a database connection

2. Select a database to use

3. Perform database query

4. Use returned data (if any)

(17)

COMMONLY USED FUNCTIONS

• mysql_connect(“hostname”,”user”,”pass”);

• mysql_select_db(“database name”, “handle”);

• mysql_query(“query”);

• mysql_num_rows(result variable from query);

Connects to MySQL server.

Selects a database to use.

Execute database query

(18)

• mysql_fetch_array(“result variable from query”);

• mysql_fetch_assoc(“result variable from query”)

Used to return several rows of the entire

results of a database query

Used to return several rows of the entire

(19)

• mysql_error();

• md5(string);

Shows the error message that has

been returned directly from MySQL server.

It uses is to encrypt a string. It returns 32 characters

(20)

md5()

• Example:

$str = “Hello”;

echo md5($str);

• Output:

(21)

date()

• Example:

echo date(“Y”); // 2015 (year)

echo date(“m”); // 12 (month)

echo date(“d”); // 27 (day)

echo date(“Y-m-d”); // 2015-01-27

echo date(“d/m/y”); // 28/12/15

echo date(“F d, Y”); // January 28, 2015

echo date(“F j, Y, h:i:s a”);

(22)

STEP 1

• <?php

// 1. Create a database connection

$connection = mysql_connect(“localhost”, “root”, “password”); if (!$connection) {

die(“Database connection failed: ” . mysql_error()); }

?>

<html> …

</html>

(23)

STEP 2

• <?php

// 1. Create a database connection

$connection = mysql_connect(“localhost”, “root”, “password”); if(!$connection) {

die(“Database connection failed: ” . mysql_error()); }

// 2. Select a database to use

$db_select = mysql_select_db(“widget_corp”, $connection); if(!$db_select) {

die(“Database selection failed: ” . mysql_error()); }

?>

<html> …

(24)

STEP 3

• Step 1 (create a database connection)

• Step 2 (select a database to use)

• <html>

<head></head> <body>

<?php

// 3. Perform database query

$result = mysql_query(“SELECT * FROM subjects”,

$connection);

if (!result) {

die(“Database query failed: ” . mysql_error()); }

?>

(25)

STEP 4

• <html> …

<?php

// 3. Perform database query

$result = mysql_query(“SELECT * FROM subjects”, $connection); if (!result) {

die(“Database query failed: ” . mysql_error()); }

// 4. Use returned data

while ($row = mysql_fetch_array($result)) { echo $row[1] . “ ” . $row[2] . “<br />”;

} ?>

(26)

STEP 5

• Step 1 • Step 2

• <html>

<head></head> <body>

Step 3 Step 4

</body> </html> <?php

// 5. Close connection

(27)

EXAMPLE

• Create a database name school in MySQL.

• Create a table name result, with following

fields sId, sName and Marks.

(28)

EXAMPLE: STEP 1

• Create connection

<?php

// 1. create a connection

$connection = mysql_connect(“localhost”,”root”,””); if(!connection) {

die(“database connection failed” . mysql_error()); }

(29)

EXAMPLE: STEP 2

• <?php

// 1. Create a database connection

$connection = mysql_connect(“localhost”, “root”, “password”); if (!$connection) {

die(“Database connection failed: ” . mysql_error()); }

// 2. Select a database to use

$db_select = mysql_select_db(“school”, $connection); if(!$db_select) {

die(“Database selection failed: ” . mysql_error()); }

?>

<html> …

(30)

EXAMPLE: STEP 3

• Step 1 (create a database connection) • Step 2 (select a database to use)

• <html>

<head></head> <body>

<?php

// 3. Perform database query

$query = “SELECT * FROM result”;

$result = mysql_query($query, $connection); if (!$result) {

die(“Database query failed: ” . mysql_error()); }

if(mysql_num_rows($result <= 0)){ die(“No record found”);

} ?>

(31)

EXAMPLE: STEP 4

• Step 1 (create a database connection) • Step 2 (select a database to use)

• <html> …

<?php

Step 3 (perform database query)

// 4. Use returned data

while ($row = mysql_fetch_array($result)) { echo $row[0] . “<br />”;

echo $row[1] . “<br />”; echo $row[2] . “<br />”; }

?>

(32)

EXAMPLE: STEP 5

• Step 1 • Step 2 • <html>

<head></head> <body>

Step 3 Step 4 </body> </html> <?php

// 5. Close connection

References

Related documents

Dedicated application for data visualization and analysis Mimic screen Data table Alarm table Waveform view Spectrum view Trend view · · · · · · · · · ·

An inability to pay the money bail may coerce people to plead guilty so that they can get out of jail sooner despite being innocent.. Up to 25

ƒ Preferential treatment of voice over data ƒ Minimize jitter and packet loss for VoIP traffic ƒ Efficient use of wireless medium. • Fast roaming support is

 After Apache is configured, every file with an extension of php or phps (=PHP source code) is processed by C:/php/php.exe before the output is sent to the browser. For displaying the

applications are CRUD based, we create an MYSQL database table for CRUD application using a PHP script.. Are you sure you want

Speare Code Editor can be easily extended to support any type of PHP development including Web applications that based on frameworks such as Drupal, Zend Framework, CodeIgniter and

If i would like, php code in my first post is possible with sample php curl code running properly describe the process the php curl to provide sample working n numbers and