Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null."— Presentation transcript:

1 PHP MySQL

2 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

3 SQL: Tables Field TypeDescription TINYINTSmall Integer Number SMALLINTSmall Integer Number MEDIUMINTInteger Number INTInteger Number VARCHARText (maximum 255 characters) TEXTText

4 SQL: Tables: Example CREATE TABLE birthdays ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), Name VARCHAR(30), Birthday VARCHAR(7) )

5 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’

6 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'

7 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 than one connection then use returned parameter to identify connection for later mysql 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.

8 Example <? $user="username"; $password="password"; $database="database"; mysql_connect(“localhost”,$user,$password); @mysql_select_db($database) or die( "Unable to select database");... mysql_close(); ?>

9 Example 2 <? $link = mysql_connect( "localhost", $_POST['username'], $_POST['password']) or die ("Connect Error: ".mysql_error()); print "Successfully connected.\n"; mysql_close($link); ?>

10 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(). mysql_affected_rows()

11 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 " Database Output "; $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 Mobile: $mobile E-mail: $email Web: $web "; ++$i; } ?>

12 Example (Title Here) <?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. "; print " \n"; while ($get_info = mysql_fetch_row($result)){ print " \n"; foreach ($get_info as $field) print "\t $field \n"; print " \n"; } print " \n"; mysql_close($link); ?>


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

Similar presentations


Ads by Google