Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. <?php
  2. class DB {
  3. protected $db_name = 'xowidos_7gx';
  4. protected $db_user = 'xowidos_tao';
  5. protected $db_pass = 'c0ntrasena';
  6. protected $db_host = 'localhost';
  7.  
  8. //open a connection to the database. Make sure this is called
  9. //on every page that needs to use the database.
  10. public function connect() {
  11. $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
  12. mysql_select_db($this->db_name);
  13.  
  14. return true;
  15. }
  16.  
  17. //takes a mysql row set and returns an associative array, where the keys
  18. //in the array are the column names in the row set. If singleRow is set to
  19. //true, then it will return a single row instead of an array of rows.
  20. public function processRowSet($rowSet, $singleRow=false)
  21. {
  22. $resultArray = array();
  23. while($row = mysql_fetch_assoc($rowSet))
  24. {
  25. array_push($resultArray, $row);
  26. }
  27.  
  28. if($singleRow === true)
  29. return $resultArray[0];
  30.  
  31. return $resultArray;
  32. }
  33.  
  34. //Select rows from the database.
  35. //returns a full row or rows from $table using $where as the where clause.
  36. //return value is an associative array with column names as keys.
  37. public function select($table, $where) {
  38. $sql = "SELECT * FROM `$table` WHERE $where";
  39. //echo $sql;
  40. $result = mysql_query($sql);
  41. if(mysql_num_rows($result) == 1){
  42. return $this->processRowSet($result, true);
  43. } else {
  44. echo mysql_error();
  45. }
  46.  
  47. return $this->processRowSet($result);
  48. }
  49.  
  50. //selecciona usuarios activos, creo...
  51. public function activos($table, $where) {
  52. $sql = "SELECT id FROM $table WHERE $where";
  53. $result = mysql_query($sql);
  54. if(mysql_num_rows($result) == 1)
  55. return $this->processRowSet($result, true);
  56.  
  57. return $this->processRowSet($result);
  58. }
  59. //Updates a current row in the database.
  60. //takes an array of data, where the keys in the array are the column names
  61. //and the values are the data that will be inserted into those columns.
  62. //$table is the name of the table and $where is the sql where clause.
  63. public function update($data, $table, $where) {
  64. foreach ($data as $column => $value) {
  65. $sql = "UPDATE $table SET $column = $value WHERE $where";
  66. mysql_query($sql) or die(mysql_error());
  67. }
  68. return true;
  69. }
  70.  
  71. //Inserts a new row into the database.
  72. //takes an array of data, where the keys in the array are the column names
  73. //and the values are the data that will be inserted into those columns.
  74. //$table is the name of the table.
  75. public function insert($data, $table) {
  76.  
  77. $columns = "";
  78. $values = "";
  79.  
  80. foreach ($data as $column => $value) {
  81. $columns .= ($columns == "") ? "" : ", ";
  82. $columns .= $column;
  83. $values .= ($values == "") ? "" : ", ";
  84. $values .= $value;
  85. }
  86.  
  87. $sql = "insert into $table ($columns) values ($values)";
  88.  
  89. //echo $sql;
  90. if(mysql_query($sql)){
  91. return mysql_insert_id();
  92. } else {
  93. $error = mysql_error();
  94. return $error;
  95. //echo utf8_encode('Tus datos ya existían en nuestra base de datos, y son atendidos por:');
  96. }
  97.  
  98. //return the ID of the user in the database.
  99.  
  100. //echo'wtf13.7<br />';
  101. }
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement