PHP

We ran the first script once only with the command php use_textfile.php. We tested the other scripts from the command line before calling them from the web application on the preceding page.

use_textfile.php

<?php  
$db_host        = 'localhost'; 
$db_user        = 'student'; 
$db_pass        = 'pp4s'; 
$db_database    = 'test'; 
$con = mysql_connect($db_host,$db_user,$db_pass); 
if (!$con) 
  { 
  die('Could not connect: ' . mysqli_error($con)); 
  } 
mysql_select_db($db_database) or die("Could not access"); 

$filename = "sql.txt";  
$file = file($filename);  
$lines = count($file);  

for ($line=0; $line<$lines; $line++){  
$sql= $file[$line];  
mysql_query($sql);  
}  
mysql_close($con); 
?> 

get_ids.php

<?php 
/* Database config */ 

$db_host        = 'localhost'; 
$db_user        = 'student'; 
$db_pass        = 'pp4s'; 
$db_database    = 'test';  

$con = mysql_connect($db_host,$db_user,$db_pass); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error($con)); 
  } 
mysql_select_db($db_database) or die("Could not access"); 
$sql="SELECT ID FROM Programmer ORDER BY Surname"; 
$result=mysql_query($sql); 
$num=mysql_numrows($result); 
$i=0; 
echo "0,"; 
while ($i < $num-1)  { 
  $current_id = mysql_result($result,$i,'ID'); 
  echo "$current_id,"; 
  $i++; 
}; 
  $current_id = mysql_result($result,$num-1,'ID'); 
echo "$current_id"; 
mysql_close($con); 
?> 

get_surnames.php

<?php 
/* Database config */ 

$db_host        = 'localhost'; 
$db_user        = 'student'; 
$db_pass        = 'pp4s'; 
$db_database    = 'test';  

$con = mysql_connect($db_host,$db_user,$db_pass); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error($con)); 
  } 
mysql_select_db($db_database) or die("Could not access database"); 
$sql="SELECT Surname FROM Programmer ORDER BY Surname"; 
$result=mysql_query($sql); 
$num=mysql_numrows($result); 
$i=0; 
echo "All,"; 
while ($i < $num-1)  { 
  $current_name = mysql_result($result,$i,'Surname'); 
  echo "$current_name,"; 
  $i++; 
}; 
  $current_name = mysql_result($result,$num-1,'Surname'); 
echo "$current_name"; 
mysql_close($con); 
?> 

get_programs.php

<?php 
/* Database config */ 

$db_host        = 'localhost'; 
$db_user        = 'student'; 
$db_pass        = 'pp4s'; 
$db_database    = 'test';  

$con = mysql_connect($db_host,$db_user,$db_pass); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error($con)); 
  } 
mysql_select_db($db_database) or die("Could not access"); 
$q = intval($_GET['q']); 
$sql="SELECT * FROM Programs WHERE ProgrammerID = '".$q."'"; 
if  ($q ==0) 
  { 
    $sql="SELECT * FROM Programs"; 
  } 
$result=mysql_query($sql); 
$num=mysql_numrows($result); 
echo "<table border='5'> 
<tr> 
<th>Program Name</th> 
<th>Description</th> 
<th>Category</th> 
</tr>"; 
$url_pre = '<a href = "http://www.pp4s.co.uk/main/prog-'; 
$url_post = '.html" target="_blank">'; 
$i=0; 
while ($i < $num)  { 
  $current_url = mysql_result($result,$i,'URL'); 
  $current_name = mysql_result($result,$i,'Name'); 
  $current_desc = mysql_result($result,$i,'Description'); 
  $current_cat = mysql_result($result,$i,'Category');  
  echo "<tr>"; 
  echo "<td>$url_pre$current_url$url_post$current_name</a></td>"; 
  echo "<td>$current_desc</td>";  
  echo "<td>$current_cat</td>"; 
  echo "</tr>"; 
  $i++; 
}; 
echo "</table>"; 
mysql_close($con); 
?> 

Programming - a skill for life!

Loading data from a MySQL database on the website