php mysql. sql: tables create table tablename { fieldname type(length) extra info,... } extra info:...

12
PHP MySQL

Upload: barnard-tyler

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

PHP

MySQL

Page 2: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

SQL: Tables

CREATE TABLE tablename {fieldname type(length) extra info,...}

Extra info: – NULL (allows nulls in this field)

– Not NULL (null is not allowed value for this field)

– AUTO_INCREMENT (value=biggest value in this field among existing +1

Page 3: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

SQL: Tables

Field TypeDescription

TINYINT Small Integer Number

SMALLINT Small Integer Number

MEDIUMINT Integer Number

INT Integer Number

VARCHAR Text (maximum 255 characters)

TEXT Text

Page 4: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

SQL: Tables: Example

CREATE TABLE birthdays (

id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

Name VARCHAR(30),

Birthday VARCHAR(7)

)

Page 5: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

SQL

Select * or list of fields From table_name [Where conditions]

Select * from birthdays

Select Name from birthdays where id>12 And birthday=‘June4’

Delete from table_name [Where conditions]Delete from birthdays where name=‘Peggy’

Page 6: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

SQL

Insert Into table_name [(fields list)] VALUES(fields values)

INSERT INTO birthdays (name, birthday) VALUES ('Peggy', 'June4')

Update table_name Set [field=value,...] [Where conditions]

UPDATE birthdays SET birthday=‘2003.01.10’ WHERE name='Peggy'

Page 7: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

PHP/MySQL: Connection

• resource mysql_connect ( [string server [, string username [, string password]]])

Open a connection to a MySQL Server . If you would like to use more thanone connection then use returned parameter to identify connection for latermysql functions calls.

• bool mysql_select_db ( string database_name [, link_identifier]) sets the current active database on the server that's associated with the specified link identifier. If no link identifier is specified, the last opened link is assumed. If no link is open, the function will try to establish a link as if mysql_connect() was called without arguments, and use it.

• bool mysql_close ( [link_identifier])Close connection that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used.

Page 8: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

Example

<?$user="username";$password="password";$database="database";

mysql_connect(“localhost”,$user,$password);@mysql_select_db($database) or die( "Unable to select database");...mysql_close();

?>

Page 9: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

Example 2

<?$link = mysql_connect( "localhost", $_POST['username'], $_POST['password']) or die ("Connect Error: ".mysql_error());

print "Successfully connected.\n";

mysql_close($link);

?>

Page 10: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

PHP/MySQL: Working with• resource mysql_query(string $query[, link_identifier])

sends a query to the currently active database on the server that's associated with the specified link identifier.

• mixed mysql_result ( resource result, int row [, mixed field])Returns the contents of one cell from a MySQL result set. $variable=mysql_result($result,$i,"fieldname");$variable=mysql_result($result,2,0); //Returns a value of the first column value of the third record

• int mysql_num_rows ( resource result) Returns the number of rows in a result set. PS:This command is only valid for SELECT statements. To retrieve the number of rows affected by a INSERT, UPDATE or DELETE query, use mysql_affected_rows().

Page 11: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

Example<?

$username=“u"; $password=“p"; $database=“db";mysql_connect(localhost,$username,$password);@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM contacts";$result=mysql_query($query);$num=mysql_num_rows($result);mysql_close();

echo "<b><center>Database Output</center></b><br><br>"; $i=0;while ($i < $num) {$phone=mysql_result($result,$i,"phone");$email=mysql_result($result,$i,"email");$web=mysql_result($result,$i,"web");

echo "Phone: $phone<br>Mobile: $mobile<br>E-mail: $email<br>Web: $web<br><hr><br>";++$i;}

?>

Page 12: PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null

Example• <html><head><title>(Title Here)</title></head><body>

<?php$db="mydatabase";$link = mysql_connect("localhost");if (! $link) die("Couldn't connect to MySQL");mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error());

$result = mysql_query("SELECT * FROM birthdays") or die("SELECT Error: ".mysql_error());$num_rows = mysql_num_rows($result);print "There are $num_rows records.<P>";print "<table width=200 border=1>\n";while ($get_info = mysql_fetch_row($result)){ print "<tr>\n";

foreach ($get_info as $field) print "\t<td><font face=arial size=1/>$field</font></td>\n";

print "</tr>\n";}print "</table>\n";

mysql_close($link);?></body></html>