Top Banner
MySQL PHP Web Technology
14

MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Jan 01, 2016

Download

Documents

Lawrence Watson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

MySQL

PHP

Web Technology

Page 2: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Logging in to Command Line

• Start -> Programs -> AppServ -> MySQL Command Line Client

• Enter Password, then you’ll be working as root.

Page 3: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Browsing Databases

• Some useful commands to begin with– status;– show databases; - show all databases– use <database_name>; – show tables;– desc <table_name>– create database <database_name>

• Create a new database

CREATE DATABASE webtech

Page 4: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Create a User

• It is a good idea to create a username to manage tables in new created database

• Proper privileges can be granted to a particular user so that only a user who has right access can manage the table

GRANT <previledge> [(col1, col2, … colN)] ON database.[table] TO user@host IDENTIFIED BY 'passwd';

GRANT select ON webtech.tct_phone TO tct@localhost IDENTIFIED BY ‘tct';

Page 5: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

MySQL Privilege Scope

Page 6: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Create a Table

CREATE TABLE tct_phone (STD_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,STD_FNAME VARCHAR( 64 ) NOT NULL ,STD_LNAME VARCHAR( 64 ) NOT NULL ,STD_PHONE VARCHAR( 12 ) NOT NULL

);

CREATE TABLE <table_name> (column_name1 <col_type> ….., ............

)

Page 7: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

SELECT Statements

• Select all records (rows) from a table

• Select some columns of all records from a table

SELECT * FROM <table_name>;

SELECT col1, col2,….colx FROM <table_name>;

SELECT * FROM tct_phone;

SELECT std_id, std_fname, std_lname FROM tct_phone;

Page 8: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

SELECT Statements (cont.)

• Select some records from a table

• WHERE clause could be any boolean expression

• ORDER BY clause (either asc or desc)

SELECT * FROM tct_phone WHERE std_id > 20;

SELECT * FROM <table_name> WHERE <condition>;

SELECT * FROM tct_phone WHERE std_id > 20 and std_fname like ‘sor%’;

SELECT * FROM tct_phone WHERE std_id > 20 and std_fname like ‘sor%’ ORDER BY std_fname desc;

Page 9: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Connecting to Database Using PHP

$hostname = “localhost”;$dbUser = “tct”;$dbPass = “tct”;$conn = mysql_connect($hostname, $db_user, $password) or die(“Cannot open connection”);

mysql_connect(HOSTNAME, USER, PASSWD);

Page 10: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Selecting a Database

mysql_select_db(“webtech”, $conn ) or die ("Cannot open database");

mysql_select_db(DATABASE, CONNECTION);

Page 11: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Making Query

• Making query to opened database

• Checking the number of fields from the query

• Checking the number of records we get

mysql_query($query);

mysql_num_fields($result)

mysql_affected_rows()

Page 12: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

How to Retrieve the Records

• There are a number of ways to get them:$name_row = mysql_fetch_row($result)

$row = mysql_fetch_object($result)

$row = mysql_fetch_array($result)

while ($name_row = mysql_fetch_row($result)) print("$name_row[0] $name_row[1] $name_row[2] <BR>\n");

while ($row = mysql_fetch_object($result)) print("$row->std_id -> $row->std_fname $row->std_lname<BR>\n");

while ($row = mysql_fetch_array($result)) print($row[std_id '].$row[std_fname '].$row[std_lname'] <BR>\n");

Page 13: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.

Example Code$conn = mysql_connect(‘localhost’, ‘tct’, ‘tct’) or die("Cannot open connection");mysql_select_db(“webtech”, $conn ) or die ("Cannot open database");mysql_db_query("tct_phone","SET NAMES utf8");//Use UTF8 for Thai font

$query = "select * from tct_phone";

$result = mysql_query($query);$num_fields = mysql_num_fields($result);

echo "<TABLE border=1>";echo "<TR>";for($i=0; $i < $num_fields; $i++) echo "<TH>".mysql_field_name($result, $i)."</TH>";echo "</TR>";while ($name_row = mysql_fetch_row($result)) {

echo "<TR>";for($i=0; $i < $num_fields; $i++) print("<TD>$name_row[$i] </TD>");echo "</TR>";

}echo "</table>";

Page 14: MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.