Download presentation
Presentation is loading. Please wait.
Published byEmily Wells Modified over 9 years ago
1
Creating Databases for Web Applications SQL Select extras Listing [names of] tables generalized display of recordset simple password handling php and other databases Homework: work on projects, new & old posting assignments
2
GetImageSize example Addition to code shown last time: $size=GetImageSize($file); print ("Dimensions are: ".$size[0]." by ".$size[1]." pixels. "); $area = $size[0]*$size[1]; print ("Area is $area pixels. ");
3
results (first part) uploading file named 91940.jpg File size is 56866 fullname is: D:\\InetPub\\wwwroot\\users\\jeanine \\\91940.jpg. Dimensions are: 600 by 393 pixels. Area is 235800 pixels. file successfully uploaded.
4
Select operators =, >, =, < IS NOT NULL, IS NULL BETWEEN IN, NOT IN LIKE (has wild card character: %, others) REGEXP Also, have DISTINCT SELECT DISTINCT category FROM questions;
5
Select aggregate functions AVG, COUNT, MIN, MAX, STD, SUM SELECT AVG(score) in players; SELECT COUNT(*) in players WHERE score > 100; Get these as 0 th field, 0 th row of recordset SELECT AVG(score), MIN(score), MAX(score), STD(score), COUNT(score) in players; Get these as 0 th, 1 st, 2 nd, 3 rd, 4 th, 5 th fields of 0 th row of recordset
6
Select control grouping SELECT order_id, SUM(quantity) FROM ordereditems GROUP BY order_id; limit: SELECT product_id, quantity FROM ordereditems LIMIT 10; limit: starting from 1 st record fitting conditions and returning 10 records SELECT product_name, product_description, product_cost FROM catalog LIMIT 1, 10; For paging, repeat with variables indicating 1 st and last entries: "SELECT product_name, product_description, product_cost FROM catalog LIMIT $FIRST, 10"
7
SELECT order_id, SUM(quantity) FROM ordereditems GROUP BY order_id; Query result: 111 29 115 126 143 224 235 Original data
8
What are tables in given database Show table names <?php require("jeanine\quizphp\opendbq.php"); $query="show tables"; $rs=mysql_db_query($DBname, $query, $link); ?> Table names <? while ($row=mysql_fetch_array($rs)){ print(" "); print($row[0]); print(" "); } print(" "); ?>
9
result Table names catalog customers history ordereditems orders players questions
10
Show table names and field names <?php require("jeanine\quizphp\opendbq.php"); $query="show tables"; $rs=mysql_db_query($DBname, $query, $link); ?> Table names <? $i = 0; while ($row=mysql_fetch_array($rs)){ print(" "); $tablenames[$i] = $row[0]; $i++; print($row[0]); print(" "); } print(" ");
11
for ($j=0;$j<$i;$j++) { $query = "describe ".$tablenames[$j]; print (" ". $tablenames[$j]. " table \n "); print (" Field Type Null Key \n "); $rt=mysql_db_query($DBname,$query,$link); while ($fi=mysql_fetch_array($rt)) { print (" ". $fi['Field']. " \n "); print (" ".$fi['Type']. " \n "); print (" ".$fi['Null']. " \n "); print (" ".$fi['Key']. " \n "); print (" "); } print (" "); } ?>
13
Table of queries If you have a large set of fixed SQL queries, you may make a new table: iddescriptiontext 1final diagnosis when presenting signs of appendicitis Select final.diagnosis from final, initial where initial.temp > 100 AND initial.pain = 'left' AND final.caseno = initial.caseno 2initial potential ulcer casesSelect * from initial where initial.pain = 'sharp' AND initial.temp < 100 ….
14
Present to user Pick selection: description final diagnosis when presenting signs of appendicitis initial potential ulcer cases Don't show the user the messy SQL
15
Produce responses Make the query the SQL corresponding to the user's choice. Display recordset in a table –Now, need generalized code that creates headings for tables and extracts names of fields 'on the fly' based on information in recordset. php: –mysql_fetch_field –mysql_fetch_array
16
Current Favorites <?php require("openfirstdb.php"); $query="Select * from favorites"; $result=mysql_db_query($DBname, $query, $link); $fieldnames= Array(); print (" "); $nf = mysql_num_fields($result); for ($i=0; $i<$nf;$i++) { $fieldobj= mysql_fetch_field($result); $fieldnames[$i]=$fieldobj->name; print (" ".$fieldnames[$i]." "); } print (" \n"); while ($row=mysql_fetch_array($result)) { print (" "); for ($i=0; $i<$nf; $i++) { print (" ".$row[$fieldnames[$i]]." "); } print(" "); } mysql_close($link); ?> first for loop to set up headers Second for loop, in while loop, to extract field data.
18
asp version recordset.fields.count recordset.fields(i).Name
19
Input and submit questions to quizasp db <% var sq ="SELECT * from favorites"; rs=Server.CreateObject("ADODB.RecordSet"); rs.Open (sq,Conn, 1,3); var fieldnames= new Array(); Response.Write (" "); var nf = rs.fields.count; var nr=rs.RecordCount; for (i=0; i<nf; i++) { fieldnames[i]=rs.fields(i).Name; Response.Write(" "+ fieldnames[i] +" "); } Response.Write (" \n"); while(!rs.EOF) { Response.Write(" "); for (j=0; j<nf; j++) { Response.Write (" "+rs.fields.item(fieldnames[j])+" "); } Response.Write(" "); rs.move(1); } %>
20
Authentication using passwords Technique is to establish a table of stored user names and encrypted passwords one way encrpytion –php's crypt or MySql's password use SQL statement that counts the number of records with the pair of values. If count is greater than 0, then the person is accepted. Use session variables or cookies to check that user is 'authenticated'. Separate procedure for storing values.
21
php: crypt Can be used with or without a seed: $cipher = crypt($password,$seed); You need to make sure that the seed is the same!
22
SQL $query = "Select count(*) from passtable where name = '$user' and pass = '$cypher'"; $result=mysql_query($Dbname,$query); $count = mysql_result($result,0,0); if ($count>0) { ….okay} else { …. no good } calculated value. recordset has one row, one field
23
php and other databases php and MySQL have a special set of functions. There are also special sets for some other databases. –show some Oracle code Alternative is to use a general API (application programming interface). –ODBC: open database connectivity –ADODB: active data object data base –?
24
<?php PutEnv("ORACLE_SID=ORASID"); $connection = Ora_Logon ("username","password"); if ($connection == false){ echo Ora_ErrorCode($connection).": ".Ora_Error($connection)." "; exit; } $cursor = Ora_Open ($connection); if ($cursor == false){ echo Ora_ErrorCode($connection).": ".Ora_Error($connection)." "; exit; } $query = "select * from email_info"; $result = Ora_Parse ($cursor, $query); if ($result == false){ echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)." "; exit; }
25
$result = Ora_Exec ($cursor); if ($result == false){ echo Ora_ErrorCode($cursor).": ".Ora_Error($cursor)." "; exit; } echo " "; echo " Full Name Email Address "; while (Ora_Fetch_Into ($cursor, &$values)){ $name = $values[0]; $email = $values[1]; echo " $name $email "; } echo " "; Ora_Close ($cursor); Ora_Logoff ($connection); ?>
26
ODBC $connect = odbc_connect("firstdb", "", ""); // no user, no password $query = "SELECT title, description FROM favorites"; $result = odbc_exec($connect, $query); print (" \n"); while(odbc_fetch_row($result)){ print (" "); print(odbc_result($result, 1)." "); print (odbc_result($result, 2)." "); } print (" ");odbc_close($connect); DSN Index starts at 1
27
ADODB <? include('adodb.inc.php'); $conn = &ADONewConnection('access'); $conn->PConnect('firstdb'); $query = "Select title, description from favorites"; $recordSet = &$conn->Execute($query); while (!$recordSet->EOF) { print $recordSet->fields[0].' '.$recordSet->fields[1].' '; $recordSet->MoveNext(); } $recordSet->Close(); $conn->Close(); ?> ADODB needs to be installed Note -> syntax Note & syntax
28
ADODB functions Metatypes for handling different names for types (char versus string, others) functions for handling dates debugging help Source: http://php.weblogs.com/ADODB_manual#install
29
Homework Post constructive comments on other projects (as a reply to posting announcing project). Post comments on php versus asp/JavaScript, MySql versus Access, Open Source versus proprietary/Microsoft. Finish* projects.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.