full php mysql connection script


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. //mysql connect
  3.  
  4. //connect to db and insert
  5. mysql_connect(localhost,$username,$password);
  6. mysql_select_db($database) or die( "Unable to select database");
  7. $query = "INSERT INTO contacts VALUES ('','John','Smith','01234 567890','00112 334455','01234 567891','[email protected]','http://www.gowansnet.com')";
  8. mysql_query($query);
  9. echo "Record added";
  10.  
  11.  
  12.  
  13.  
  14. //mysql select query
  15. $result = mysql_query("SELECT id,name FROM table");
  16. if(!$result) {
  17. echo mysql_error();
  18. }
  19. while($row = mysql_fetch_assoc($result)) {
  20. echo "Row data: " . $row['id'] . " - " . $row['name'];
  21. }
  22.  
  23. //alternative select query
  24. mysql_connect(localhost,$username,$password);
  25. mysql_select_db($database) or die( "Unable to select database");
  26. $query="SELECT * FROM contacts";
  27. $result=mysql_query($query);
  28. echo "Data displayed";
  29.  
  30.  
  31. //mysql update
  32. mysql_connect(localhost,$username,$password);
  33. mysql_select_db($database) or die( "Unable to select database");
  34. $query = "UPDATE contacts SET
  35. first = '$ud_first',
  36. last = '$ud_last',
  37. phone = '$ud_phone',
  38. mobile = '$ud_mobile',
  39. fax = '$ud_fax',
  40. email = '$ud_email',
  41. web = '$ud_web'
  42. WHERE id = '$ud_id'";
  43. mysql_query($query);
  44. echo "Record Updated";
  45.  
  46.  
  47.  
  48. //delete contact
  49. mysql_connect(localhost,$username,$password);
  50. mysql_select_db($database) or die( "Unable to select database");
  51. $query = "DELETE FROM contacts WHERE id='$id'";
  52. mysql_query($query);
  53. echo "Record Deleted";
  54.  
  55.  
  56. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.