MySQL PHP Web Technology
Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working as root.
Browsing Databases Some useful commands to begin with –status; –show databases; - show all databases –use ; –show tables; –desc –create database Create a new database CREATE DATABASE webtech
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 [(col1, col2, … colN)] ON database.[table] TO IDENTIFIED BY 'passwd'; GRANT select ON webtech.tct_phone TO IDENTIFIED BY ‘tct';
MySQL Privilege Scope
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 ( column_name1 ….., )
SELECT Statements Select all records (rows) from a table Select some columns of all records from a table SELECT * FROM ; SELECT col1, col2,….colx FROM ; SELECT * FROM tct_phone; SELECT std_id, std_fname, std_lname FROM tct_phone;
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 WHERE ; 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;
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);
Selecting a Database mysql_select_db(“webtech”, $conn ) or die ("Cannot open database") ; mysql_select_db(DATABASE, CONNECTION);
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()
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] \n"); while ($row = mysql_fetch_object($result)) print("$row->std_id -> $row->std_fname $row->std_lname \n"); while ($row = mysql_fetch_array($result)) print($row[std_id '].$row[std_fname '].$row[std_lname'] \n");
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 " "; for($i=0; $i ".mysql_field_name($result, $i)." "; echo " "; while ($name_row = mysql_fetch_row($result)) { echo " "; for($i=0; $i $name_row[$i] "); echo " "; } echo " ";