php mysql. sql: tables create table tablename { fieldname type(length) extra info,... } extra info:...
TRANSCRIPT
PHP
MySQL
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
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
SQL: Tables: Example
CREATE TABLE birthdays (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
Name VARCHAR(30),
Birthday VARCHAR(7)
)
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’
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'
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.
Example
<?$user="username";$password="password";$database="database";
mysql_connect(“localhost”,$user,$password);@mysql_select_db($database) or die( "Unable to select database");...mysql_close();
?>
Example 2
<?$link = mysql_connect( "localhost", $_POST['username'], $_POST['password']) or die ("Connect Error: ".mysql_error());
print "Successfully connected.\n";
mysql_close($link);
?>
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().
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;}
?>
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>