Advertisement
wkerswell

Connect code

Mar 4th, 2013
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php
  2. //Shows the the errors if there are any for debugging
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', '1');
  5.  
  6.     //create empty strings to stop undefined errors
  7.     $results = "";
  8.    
  9. $host="mysql5";
  10. $user="fet10027875";
  11. $password="ibizalad";
  12.  
  13. $connection = mysql_connect($host,$user,$password);
  14.  
  15. if (!$connection)
  16.  
  17.  {
  18.  die("Couldn't connect to server");
  19.  }
  20.  
  21. else
  22.  
  23. {
  24.  
  25.  $name=$_POST[name];
  26.  
  27.  // select database
  28.  
  29. $db_selected = mysql_select_db($user, $connection);
  30.  
  31. if (!$db_selected)
  32.  
  33. {
  34.     die ('Can\'t use $user : ' . mysql_error());
  35. }
  36.  
  37.   $query = "select name from computer, app where computer.code = app.code and app.app = '$name'";
  38.      
  39.   $result = mysql_query($query)
  40.     or die ("Couldn't execute query");
  41.    
  42.     while ($row = mysql_fetch_array ($result))
  43.     {
  44.         extract ($row);
  45.         echo "<p>$name</p>";
  46.     }
  47.  
  48.  }
  49.  
  50. mysql_close($connection);
  51.    
  52.     //start the first part of the Query
  53.     $sql = "SELECT * FROM Drivers WHERE ";
  54.    
  55.  
  56.  
  57. if( isset($_POST['SubmitDrivers']) )
  58. {
  59.     //Add title to the results var
  60.     $results = "<h1><u>Driver</u></h1>";
  61.    
  62.     //if the drivers feild is set and the content lenght is greater than 0
  63.     if( isset($_POST['driverName']) && strlen($_POST['driverName']) > 0)
  64.     {
  65.         //set the posted var = to local var
  66.         $driversName = $_POST['driverName'];
  67.    
  68.         //Add the driver clause to the sql string
  69.         $sql = $sql."First_Name='".$driversName."' AND ";
  70.        
  71.     }
  72.    
  73.     if( isset($_POST['driversLast']) && strlen($_POST['driversLast']) > 0 )
  74.     {
  75.         $driversLast = $_POST['driversLast'];
  76.    
  77.         //search for the driver
  78.         $sql = $sql."Second_Name='".$driversLast."' AND ";
  79.        
  80.     }
  81.     if( isset($_POST['driverDoB']) && strlen($_POST['driverDoB']) > 0 )
  82.     {
  83.         $driversDOB = $_POST['driverDoB'];
  84.    
  85.         //search for the driver
  86.         $sql = $sql."DOB='".$driversDOB."' AND ";
  87.        
  88.     }
  89.     if( isset($_POST['driverNationality']) && strlen($_POST['driverNationality']) > 0  )
  90.     {
  91.         $driverNationality = $_POST['driverNationality'];
  92.    
  93.         //search for the driver
  94.         $sql = $sql."Nationality='".$driverNationality."' AND ";
  95.        
  96.     }
  97.    
  98.     //your query is without the extra and bit on the end
  99.     $query = substr_replace($sql ,"",-4);
  100.    
  101.     //link to db
  102.     //$link = mysql_connect(/*arguments here*/);
  103.  
  104.  
  105.    
  106.     //gets the results of the query
  107.     //$result = mysql_query($query, $link);
  108.  
  109.     //if there are results
  110.    /*if ($result) {
  111.         //loop over the results
  112.       while($row = mysql_fetch_array($result)) {
  113.         //output the results here
  114.         $firstName = $row[First_Name];
  115.       }
  116.  
  117.     }
  118.     else {
  119.       echo mysql_error();
  120.     }
  121.    
  122.      //connect to db
  123.      
  124.      
  125.      //proccess the results
  126.      
  127.     //Display the results*/
  128.    
  129. }
  130.  
  131.  
  132. //shows the sql string
  133. echo $results;
  134. echo $query;
  135. // Includes the footer php, so only has to ba changed once
  136. ?>
  137. <?php include 'foot.php'; ?>
  138. sorry wrong code
  139. this is the one
  140. <html>
  141.  
  142.  
  143. <head>
  144.  
  145. <!--This is a comment. Comments are not displayed in the browser-->
  146.  
  147. <title>PHP MySQL</title>
  148.  
  149. </head>
  150.  
  151.  
  152. <body>
  153.  
  154.   <h1> This is the response:  </h1>
  155.  
  156. <?php
  157.  
  158. // Don't pass these details to students!
  159.  
  160. $host="mysql5";
  161. $user="username";
  162. $password="password";
  163.  
  164. $connection = mysql_connect($host,$user,$password);
  165.  
  166. if (!$connection)
  167.  
  168.  {
  169.  die("Couldn't connect to server");
  170.  }
  171.  
  172. else
  173.  
  174. {
  175.  
  176.  $name=$_POST[name];
  177.  
  178.  // select database
  179.  
  180. $db_selected = mysql_select_db($user, $connection);
  181.  
  182. if (!$db_selected)
  183.  
  184. {
  185.     die ('Can\'t use $user : ' . mysql_error());
  186. }
  187.  
  188.   $query = "select name from computer, app where computer.code = app.code and app.app = '$name'";
  189.      
  190.   $result = mysql_query($query)
  191.     or die ("Couldn't execute query");
  192.    
  193.     while ($row = mysql_fetch_array ($result))
  194.     {
  195.         extract ($row);
  196.         echo "<p>$name</p>";
  197.     }
  198.  
  199.  }
  200.  
  201. mysql_close($connection);
  202.  
  203.  ?>
  204.  
  205.  </body>
  206.  
  207. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement