<?php// Sample2 Script$link=mysqli_connect('itins3.matcmadison.edu','astudent','phpmysql','northwind');if(!$link){exit('Connection to MySQL server failed!');}$query='SELECT CompanyName, Phone from Shippers';$result=mysqli_query($link,$query);if(!$result){die('Query failed: '.mysqli_error($link));}$record=mysqli_fetch_assoc($result);foreach($recordas$key=>$value){?><?="$key: $value"?></br><?php}echo'<br />';mysqli_close($link);
In the following code know that the regular expression is looking for and what results will be returned from the two regular expression functions.
NOTE: Look up items in the PHP Manual if you're not sure what they are or how they work!
<html> <head><title>Sample5 Script</title></head> <body bgcolor="lightgreen"><?php$name="John Doe";$address="1001 Logic Dr.";$email="johndoe@place.gov";$title="VP";$outputstring="$name\t$address\t$email\t$title\n";echo'The data to be written:<br />';echo$outputstring.'<br>';$filename=$_SERVER['DOCUMENT_ROOT'].'/files/info.txt';$filehandle=fopen($filename,"ab");if(fwrite($filehandle,$outputstring,strlen($outputstring))==FALSE){echo"You cannot write to $filename.<br />";}else{$text=file_get_contents($filename);echo'<br />The contents of the file is:<br />';echo"<pre>$text</pre>”; } fclose($filehandle); ?> </body></html>
Where is this script looking for the file data.txt to be located? What happens if the file isn’t there? If the file is there what does the script do?
<?php$db=mysqli_connect('localhost','root','','northwind');if(!$db){exit('Connection to server failed: '.mysqli_error($db));}$result=mysqli_query($db,'SELECT * FROM Customers');if(!$result){exit('Database query error: '.mysqli_error($db));}?><table><?phpfor($i=0;$i<mysqli_num_fields($result);$i++):?> <th><u> <?=mysqli_fetch_field_direct($result,$i)->name?> </u></th><?phpendfor;?><?phpwhile($record=mysqli_fetch_row($result)):?> <tr><?phpfor($j=0;$j<mysqli_num_fields($result);$j++):?> <td><?=$record[$j]?></td><?phpendfor;?> </tr><?phpendwhile;?></table>
In this code what is the purpose of enctype="multipart/form-data"? What is the move_uploaded_file() and what is the end result of it being run in this php script?
e2sample8.html
1 2 3 4 5 6 7 8 910111213141516
<html><head><title>Sample8 Form</title></head><bodybgcolor="lavender"><formenctype="multipart/form-data"action="sample7.php"method="POST">
Browse and select the file you want to upload: <br/><inputname="my_file"type="file"/><br/><inputtype=submitvalue="Get File"/></form></body></html>
e2sample8.php
1 2 3 4 5 6 7 8 91011121314151617181920212223
<html> <head> <title>Sample7 Script</title> </head> <body bgcolor="#33ff33"><?phpecho'The uploaded file is: ',$_FILES['my_file']['tmp_name'],'<br />';$filename=$_FILES['my_file']['name'];$filesize=$_FILES['my_file']['size'];$directory=$_SERVER['DOCUMENT_ROOT'].'/files/';$uploadFile=$directory.$filename;echo"The moved file is: $uploadFile<br />";if(move_uploaded_file($_FILES['my_file']['tmp_name'],$uploadFile)){echo'The file was successfully uploaded.<br />';echo"The size of file, $filename, is $filesize bytes.<br />";}?> </body></html>