irwan

PHP Basic : Connect, Insert, Upadate, Delete, Select.

Nov 19th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. CONNECT TO DATABSE
  2.  
  3.  
  4. <?php
  5.   //these variables are used for database connection
  6.   $host = "localhost";
  7.   $user = "db_user";
  8.   $pass = "db_pass";
  9.   $database = "db_name";
  10.  
  11.   //connect to the database.
  12.   $db = mysql_connect($host, $user, $pass);
  13.   mysql_select_db ($database);
  14. ?>
  15.  
  16.  
  17.  
  18. INSERT
  19.  
  20.  
  21. <?php
  22.   //load database connection
  23.   //...
  24.  
  25.   //we will pretend we're getting the info from a form.
  26.   $firstName = $_POST['firstName'];
  27.   $lastName = $_POST['lastName'];
  28.   $emailAddress = $_POST['email'];
  29.   $zipCode = $_POST['zip'];
  30.  
  31.   //do your error checking on the input.
  32.   //...
  33.  
  34.   //Make the query to insert into the table.
  35.   $query = "INSERT INTO `people` (`firstName`,`lastName`,`email`,`zip`, `dateAdded`)
  36.                 VALUES ('$firstName','$lastName','$emailAddress','$zipCode', NOW());";
  37.  
  38.   //run the query to insert the person.
  39.   $result = mysql_query($query) OR die(mysql_error());
  40.  
  41.   //let them know the person has been added.
  42.   echo "You have successfully added " . $firstName . " " . $lastName . " to the database.";
  43. ?>
  44.  
  45.  
  46. UPDATE
  47.  
  48.  
  49.  
  50. <?php
  51.   //load database connection
  52.   //...
  53.  
  54.   //we will pretend we're getting the info from a form.
  55.   $firstName = $_POST['firstName'];
  56.   $lastName = $_POST['lastName'];
  57.   $emailAddress = $_POST['email'];
  58.   $zipCode = $_POST['zip'];
  59.  
  60.   //we want to know who we are supposed to update.
  61.   //It would be safer to grab the userid from a cookie or session
  62.   //and check with the database to make sure it's a valid user
  63.   //and that the person is trying to update their own info and not
  64.   //someone elses info.
  65.   $userId = $_POST['userId'];
  66.  
  67.   //do your error checking on the input.
  68.   //...
  69.  
  70.   //Make the query to update the table.
  71.   $query = "UPDATE `people` SET firstName='$firstName', lastName='$lastName',
  72.                 email='$emailAddress', zip='$zipcode' WHERE userid='$userId';";
  73.  
  74.   //run the query to update the person.
  75.   $result = mysql_query($query) OR die(mysql_error());
  76.  
  77.   //let them know the person has been added.
  78.   echo "You have successfully updated " . $firstName . " " . $lastName . " in the database.";
  79. ?>
  80.  
  81.  
  82.  
  83.  
  84.  
  85. DELETE
  86.  
  87. <?php
  88.   //load database connection
  89.   //...
  90.  
  91.   //get the userid of the person we're deleting.
  92.   $userId = $_POST['userId'];
  93.  
  94.   //write the query to delete the person.
  95.   $query = "DELETE FROM `people` WHERE userid='$userId'";
  96.  
  97.   //run the query to delete the person.
  98.   $result = mysql_query($query) OR die(mysql_error());
  99.  
  100.   echo 'You successfully deleted the user.';
  101. ?>
  102.  
  103.  
  104.  
  105.  
  106. SELECT
  107.  
  108. php:
  109. <?php
  110.   //load database connection
  111.   //...
  112.  
  113.   //make the query to run.
  114.   $query = "SELECT * FROM `people`";
  115.   $result = mysql_query($query) OR die(mysql_error());
  116.  
  117.   //now we turn the results into an array and loop through them.
  118.   while($row = mysql_fetch_array($result))
  119.   {
  120.     $firstName = $row['firstName'];
  121.     $lastName = $row['lastName'];
  122.  
  123.     echo 'This is: ' . $firstName . ' ' . $lastName . "<br/>\n";
  124.   }
  125. ?>
  126.  
  127.  
  128. SELECT Statement (With ordering):
  129.  
  130. <?php
  131.   //load database connection
  132.   //...
  133.  
  134.   //make the query to run.
  135.   //Sort the last name in an ascending order (A-Z)
  136.   $query = "SELECT * FROM `people` ORDER BY lastName ASC";
  137.   $result = mysql_query($query) OR die(mysql_error());
  138.  
  139.   //now we turn the results into an array and loop through them.
  140.   while($row = mysql_fetch_array($result))
  141.   {
  142.     $firstName = $row['firstName'];
  143.     $lastName = $row['lastName'];
  144.  
  145.     echo 'This is: ' . $firstName . ' ' . $lastName . "<br/>\n";
  146.   }
  147. ?>
  148.  
  149.  
  150. SELECT Statement (Limiting results):
  151.  
  152. <?php
  153.   //load database connection
  154.   //...
  155.  
  156.   //make the query to run.
  157.   //order the date descending (most recent first) and get the last 5 entries.
  158.   $query = "SELECT * FROM `people` ORDER BY dateAdded DESC LIMIT 5";
  159.   $result = mysql_query($query) OR die(mysql_error());
  160.  
  161.   //now we turn the results into an array and loop through them.
  162.   while($row = mysql_fetch_array($result))
  163.   {
  164.     $firstName = $row['firstName'];
  165.     $lastName = $row['lastName'];
  166.  
  167.     echo 'This is: ' . $firstName . ' ' . $lastName . "<br/>\n";
  168.   }
  169. ?>
  170.  
  171.  
  172.  
  173.  
  174. SELECT Statement (with WHERE clause):
  175.  
  176. <?php
  177.   //load database connection
  178.   //...
  179.  
  180.   //make the query to run.
  181.   //UPPER makes the lastName uppercase.  This way if the first character is lowercase,
  182.   //it will still find it.
  183.   $query = "SELECT * FROM `people` WHERE UPPER(lastName) LIKE 'W%'";
  184.   $result = mysql_query($query) OR die(mysql_error());
  185.  
  186.   //now we turn the results into an array and loop through them.
  187.   while($row = mysql_fetch_array($result))
  188.   {
  189.     $firstName = $row['firstName'];
  190.     $lastName = $row['lastName'];
  191.  
  192.     echo 'This is: ' . $firstName . ' ' . $lastName . "<br/>\n";
  193.   }
  194. ?>
  195.  
  196.  
  197. SELECT Statement (Counting the results):
  198.  
  199. <?php
  200.   //load database connection
  201.   //...
  202.  
  203.   //get the username and password from the form.
  204.   $userName = $_POST['username'];
  205.   $password = $_POST['password'];
  206.  
  207.   //make the query to run.
  208.   $query = "SELECT * FROM `people` WHERE username='$userName' AND password='$password'";
  209.   $result = mysql_query($query) OR die(mysql_error());
  210.   $count = mysql_num_rows($result);
  211.  
  212.   //if count > 0, then it returned a result, meaning the info provided was valid.
  213.   //now we can welcome them.
  214.   if($count > 0) {
  215.     //since there can only be one person with that username and password,
  216.     //we don't have to loop
  217.     $row = mysql_fetch_array($result);
  218.  
  219.     $firstName = $row['firstName'];
  220.  
  221.     echo 'Welcome back, '.$firstName.'!<br/>';
  222.   }
  223.   else
  224.   {
  225.     echo 'The information you provided was not valid.';
  226.   }
  227. ?>
  228.  
  229.  
  230.  
Advertisement
Add Comment
Please, Sign In to add comment