Posted By

djstaz0ne on 04/13/10


Tagged

mysql database php oop connection zend


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
dantreacy


PHP Mysql Connection Object


 / Published in: PHP
 

A simple PHP object which connects to a mysql database, selects fields, creates users and more..

  1. <?php
  2. class dbCon{
  3. public function __construct(){
  4. $server = "host";
  5. $user = "user";
  6. $pass = "pass";
  7. $db = "db";
  8. mysql_connect($server, $user, $pass) or die("Error connecting to sql server: ".mysql_error());
  9. }
  10.  
  11. function getFields($table){
  12. $fields = $this->selectFields("*",$table);
  13. return $fields;
  14. }
  15.  
  16. function selectFields($fields,$table){
  17. $rs = mysql_query("SHOW columns FROM " . $table) or die ("cannot select table fields");
  18. $daFields = explode(",",$fields);
  19. $chosenFields = array();
  20. while($row = mysql_fetch_assoc($rs)){
  21. if($fields != "*"){
  22. $intersect = array_intersect($daFields,array($row['Field']));
  23. if(count($intersect) > 0)array_push($chosenFields, $row);
  24. }
  25. else array_push($chosenFields,$row);
  26. }
  27. array_unshift($chosenFields,$table);
  28. return $chosenFields;
  29. }
  30.  
  31. function createUser($names,$values){
  32. $na = explode(",",$names);
  33. $va = explode(",",$values);
  34. $pass = "";
  35. for($i=0; $i<count($na); $i++){
  36. if($na[$i] == "password"){
  37. $pass = str_replace("'","",$va[$i]);
  38. $values = str_replace($pass,crypt($pass),$values);
  39. }
  40. }
  41. $sql = "INSERT INTO users (".$names.") VALUES (".$values.")";
  42. mysql_query($sql) or die("Error Creating User");
  43. return "Account Successfully Created";
  44. }
  45.  
  46. function login($username,$password){
  47. $sql = "SELECT password FROM users WHERE username='".$username."'";
  48. $rs = mysql_query($sql);
  49. while($row = mysql_fetch_assoc($rs)) $cryptPass = $row['password'];
  50. $rs = mysql_query("SELECT users.id, users.first_name, users.last_name, users.email FROM users WHERE users.username='".$username."' AND users.password='".crypt($password, $cryptPass)."'");
  51. $userInfo = array();
  52. while($row = mysql_fetch_array($rs)){
  53. foreach($row as $name => $value){
  54. $userInfo[$name] = $value;
  55. }
  56. }
  57. return $userInfo;
  58. }
  59.  
  60. function userAvailable($user){
  61. $rs = mysql_query("SELECT username FROM users WHERE username='$user'");
  62. return mysql_num_rows($rs);
  63. }
  64. }
  65. ?>

Report this snippet  

You need to login to post a comment.