Posted By

paschim on 09/15/10


Tagged

mysql php


Versions (?)

full php mysql connection script


 / Published in: PHP
 

  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. }
  18. while($row = mysql_fetch_assoc($result)) {
  19. echo "Row data: " . $row['id'] . " - " . $row['name'];
  20. }
  21.  
  22. //alternative select query
  23. mysql_connect(localhost,$username,$password);
  24. mysql_select_db($database) or die( "Unable to select database");
  25. $query="SELECT * FROM contacts";
  26. $result=mysql_query($query);
  27. echo "Data displayed";
  28.  
  29.  
  30. //mysql update
  31. mysql_connect(localhost,$username,$password);
  32. mysql_select_db($database) or die( "Unable to select database");
  33. $query = "UPDATE contacts SET
  34. first = '$ud_first',
  35. last = '$ud_last',
  36. phone = '$ud_phone',
  37. mobile = '$ud_mobile',
  38. fax = '$ud_fax',
  39. email = '$ud_email',
  40. web = '$ud_web'
  41. WHERE id = '$ud_id'";
  42. mysql_query($query);
  43. echo "Record Updated";
  44.  
  45.  
  46.  
  47. //delete contact
  48. mysql_connect(localhost,$username,$password);
  49. mysql_select_db($database) or die( "Unable to select database");
  50. $query = "DELETE FROM contacts WHERE id='$id'";
  51. mysql_query($query);
  52. echo "Record Deleted";
  53.  
  54.  
  55. ?>

Report this snippet  

You need to login to post a comment.