11
Chapter 9 Using PHP with MySQL

Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266 des/header.html

Embed Size (px)

Citation preview

Page 1: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

Chapter 9

Using PHP with MySQL

Page 2: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

header.html

• Script 9.1 on page 266• http://

cscdb.nku.edu/csc301/frank/ch09/includes/header.html

• ch09\includes\header.html

Page 3: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

mysqli_connect.php

• Script 9.2 on page 269• http://cscdb.nku.edu/csc301/frank/ch09/mys

qli_connect.php• ch09\mysqli_connect.php

Page 4: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

Invalid Password

• http://cscdb.nku.edu/csc301/frank/ch09/mysqli_connect2.php

Could not connect to MySQL: Access denied for user 'frank'@'127.0.0.1' (using password: YES)

Page 5: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

Access Information

// Set the database access information as constants:DEFINE ('DB_USER', 'frank');DEFINE ('DB_PASSWORD', 123456');DEFINE ('DB_HOST', 'localhost');DEFINE ('DB_NAME', 'db_frank');

Page 6: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

mysqli_connect

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

Page 7: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

Protecting Connection Information

• Page 270 Figure B• Store connection information outside of

www.example.com (htdocs folder)• Use .php extension

Page 8: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

sql.sql

• Use only Chapter 5• USE db_frank;

Page 9: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

users table

mysql -h cscdb.nku.edu -u frank -p <sql.sqlmysql -h cscdb.nku.edu -u frank –pmysql> use db_frank;Database changedmysql> show tables;mysql> select * from users;

Page 10: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

register.php

• Script 9.3 on page 275• http://cscdb.nku.edu/csc301/frank/ch09/regis

ter.php

• ch09\script_09_03\register.php

Page 11: Chapter 9 Using PHP with MySQL. header.html Script 9.1 on page 266  des/header.html

Running MySQL Query

// Make the query:$q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";// Run the query.$r = @mysqli_query ($dbc, $q); if ($r) { // If it ran OK.