Guest User

Untitled

a guest
Jan 20th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1. Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2) in /homepages/38/d433499133/htdocs/lib/class_db.php on line 85`
  2.  
  3. <?php
  4. /**
  5. * Database Class
  6. *
  7. * @package Membership Manager Pro
  8. * @author wojoscripts.com
  9. * @copyright 2010
  10. * @version $Id: indexclass_db.php, v2.00 2011-07-10 10:12:05 gewa Exp $
  11. */
  12.  
  13. if (!defined("_VALID_PHP"))
  14. die('Direct access to this location is not allowed.');
  15.  
  16. class Database
  17. {
  18. private $server = "I put the server info here.";
  19. private $user = "I put the username here.";
  20. private $pass = "I put the password here.";
  21. private $database = "I put the db name here.";
  22. public $error = "";
  23. public $errno = 0;
  24. protected $affected_rows = 0;
  25. protected $query_counter = 0;
  26. protected $link_id = 0;
  27. protected $query_id = 0;
  28.  
  29.  
  30. /**
  31. * Database::__construct()
  32. *
  33. * @param mixed $server
  34. * @param mixed $user
  35. * @param mixed $pass
  36. * @param mixed $database
  37. * @return
  38. */
  39. function __construct($server, $user, $pass, $database)
  40. {
  41. $this->server = $server;
  42. $this->user = $user;
  43. $this->pass = $pass;
  44. $this->database = $database;
  45.  
  46. }
  47.  
  48. /**
  49. * Database::connect()
  50. * Connect and select database using vars above
  51. * @return
  52. */
  53. function connect()
  54. {
  55. $this->link_id = $this->connect_db($this->server, $this->user, $this->pass);
  56.  
  57. if (!$this->link_id)
  58. $this->error("<div style='text-align:center'>"
  59. . "<span style='padding: 5px; border: 1px solid #999; background-color:#EFEFEF;"
  60. . "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>"
  61. . "<b>Database Error:</b>Connection to Database " . $this->database . " Failed</span></div>");
  62.  
  63. if (!$this->select_db($this->database, $this->link_id))
  64. $this->error("<div style='text-align:center'>"
  65. . "<span style='padding: 5px; border: 1px solid #999; background-color: #EFEFEF;"
  66. . "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>"
  67. . "<b>Database Error:</b>mySQL database (" . $this->database . ")cannot be used</span></div>");
  68.  
  69. $this->query("SET NAMES 'utf8'", $this->link_id);
  70. $this->query("SET CHARACTER SET 'utf8'", $this->link_id);
  71. $this->query("SET CHARACTER_SET_CONNECTION=utf8", $this->link_id);
  72. $this->query("SET SQL_MODE = ''", $this->link_id);
  73.  
  74. unset($this->password);
  75. }
  76.  
  77. /**
  78. * Database::connect_db()
  79. *
  80. * @param mixed $server
  81. * @param mixed $user
  82. * @param mixed $pass
  83. * @return
  84. */
  85. function connect_db($server, $user, $pass)
  86. {
  87. return mysql_connect($server, $user, $pass);
  88. }
  89.  
  90. /**
  91. * Database::select_db()
  92. *
  93. * @param mixed $database
  94. * @param mixed $link_id
  95. * @return
  96. */
  97. function select_db($database, $link_id)
  98. {
  99. return mysql_select_db($database, $link_id);
  100. }
  101.  
  102. /**
  103. * Database::query()
  104. * Executes SQL query to an open connection
  105. * @param mixed $sql
  106. * @return (query_id)
  107. */
  108. function query($sql)
  109. {
  110. if (trim($sql != "")) {
  111. $this->query_counter++;
  112. $this->query_show .= stripslashes($sql) . "<hr size='1' />";
  113. $this->query_id = mysql_query($sql, $this->link_id);
  114.  
  115. $this->last_query = $sql . '<br />';
  116. }
  117.  
  118. if (!$this->query_id)
  119. $this->error("mySQL Error on Query : " . $sql);
  120.  
  121. return $this->query_id;
  122.  
  123. }
  124.  
  125. /**
  126. * Database::first()
  127. * Fetches the first row only, frees resultset
  128. * @param mixed $string
  129. * @return array
  130. */
  131. function first($string)
  132. {
  133. $query_id = $this->query($string);
  134. $record = $this->fetch($query_id);
  135. $this->free($query_id);
  136.  
  137. return $record;
  138. }
  139.  
  140. /**
  141. * Database::fetch()
  142. * Fetches and returns results one line at a time
  143. * @param integer $query_id
  144. * @return array
  145. */
  146. function fetch($query_id = -1)
  147. {
  148. if ($query_id != -1)
  149. $this->query_id = $query_id;
  150.  
  151. if (isset($this->query_id)) {
  152. $record = mysql_fetch_array($this->query_id, MYSQL_ASSOC);
  153. } else
  154. $this->error("Invalid query_id: <b>" . $this->query_id . "</b>. Records could not be fetched.");
  155.  
  156. return $record;
  157. }
  158.  
  159. /**
  160. * Database::fetch_all()
  161. * Returns all the results
  162. * @param mixed $sql
  163. * @return assoc array
  164. */
  165. function fetch_all($sql)
  166. {
  167. $query_id = $this->query($sql);
  168. $record = array();
  169.  
  170. while ($row = $this->fetch($query_id, $sql)) :
  171. $record[] = $row;
  172. endwhile;
  173.  
  174. $this->free($query_id);
  175.  
  176. return $record;
  177. }
  178.  
  179. /**
  180. * Database::free()
  181. * Frees the resultset
  182. * @param integer $query_id
  183. * @return query_id
  184. */
  185. function free($query_id = -1)
  186. {
  187. if ($query_id != -1)
  188. $this->query_id = $query_id;
  189.  
  190. return mysql_free_result($this->query_id);
  191. }
  192.  
  193. /**
  194. * Database::insert()
  195. * Insert query with an array
  196. * @param mixed $table
  197. * @param mixed $data
  198. * @return id of inserted record, false if error
  199. */
  200. function insert($table = null, $data)
  201. {
  202. global $core;
  203. if ($table === null or empty($data) or !is_array($data)) {
  204. $this->error("Invalid array for table: <b>".$table."</b>.");
  205. return false;
  206. }
  207. $q = "INSERT INTO `" . $table . "` ";
  208. $v = '';
  209. $k = '';
  210.  
  211. foreach ($data as $key => $val) :
  212. $k .= "`$key`, ";
  213. if (strtolower($val) == 'null')
  214. $v .= "NULL, ";
  215. elseif (strtolower($val) == 'now()')
  216. $v .= "NOW(), ";
  217. elseif (strtolower($val) == 'tzdate')
  218. $v .= "DATE_ADD(NOW(),INTERVAL " . $core->timezone . " HOUR), ";
  219. else
  220. $v .= "'" . $this->escape($val) . "', ";
  221. endforeach;
  222.  
  223. $q .= "(" . rtrim($k, ', ') . ") VALUES (" . rtrim($v, ', ') . ");";
  224.  
  225. if ($this->query($q)) {
  226. return $this->insertid();
  227. } else
  228. return false;
  229. }
  230.  
  231. /**
  232. * Database::update()
  233. * Update query with an array
  234. * @param mixed $table
  235. * @param mixed $data
  236. * @param string $where
  237. * @return query_id
  238. */
  239. function update($table = null, $data, $where = '1')
  240. {
  241. global $core;
  242. if ($table === null or empty($data) or !is_array($data)) {
  243. $this->error("Invalid array for table: <b>" . $table . "</b>.");
  244. return false;
  245. }
  246.  
  247. $q = "UPDATE `" . $table . "` SET ";
  248. foreach ($data as $key => $val) :
  249. if (strtolower($val) == 'null')
  250. $q .= "`$key` = NULL, ";
  251. elseif (strtolower($val) == 'now()')
  252. $q .= "`$key` = NOW(), ";
  253. elseif (strtolower($val) == 'tzdate')
  254. $q .= "`$key` = DATE_ADD(NOW(),INTERVAL " . $core->timezone . " HOUR), ";
  255. elseif (strtolower($val) == 'default()')
  256. $q .= "`$key` = DEFAULT($val), ";
  257. elseif(preg_match("/^inc((-?d+))$/i",$val,$m))
  258. $q.= "`$key` = `$key` + $m[1], ";
  259. else
  260. $q .= "`$key`='" . $this->escape($val) . "', ";
  261. endforeach;
  262. $q = rtrim($q, ', ') . ' WHERE ' . $where . ';';
  263.  
  264. return $this->query($q);
  265. }
  266.  
  267. /**
  268. * Database::delete()
  269. * Delete records
  270. * @param mixed $table
  271. * @param string $where
  272. * @return
  273. */
  274. function delete($table, $where = '')
  275. {
  276. $q = !$where ? 'DELETE FROM ' . $table : 'DELETE FROM ' . $table . ' WHERE ' . $where;
  277. return $this->query($q);
  278. }
  279.  
  280. /**
  281. * Database::insert_id()
  282. * Returns last inserted ID
  283. * @param integer $query_id
  284. * @return
  285. */
  286. function insertid()
  287. {
  288. return mysql_insert_id($this->link_id);
  289. }
  290.  
  291. /**
  292. * Database::affected()
  293. * Returns the number of affected rows
  294. * @param integer $query_id
  295. * @return
  296. */
  297. function affected() {
  298. return mysql_affected_rows($this->link_id);
  299. }
  300.  
  301. /**
  302. * Database::numrows()
  303. *
  304. * @param integer $query_id
  305. * @return
  306. */
  307. function numrows($query_id = -1)
  308. {
  309. if ($query_id != -1)
  310. $this->query_id = $query_id;
  311.  
  312. $this->num_rows = mysql_num_rows($this->query_id);
  313. return $this->num_rows;
  314. }
  315.  
  316. /**
  317. * Database::fetchrow()
  318. * Fetches one row of data
  319. * @param integer $query_id
  320. * @return fetched row
  321. */
  322. function fetchrow($query_id = -1)
  323. {
  324. if ($query_id != -1)
  325. $this->query_id = $query_id;
  326.  
  327. $this->fetch_row = mysql_fetch_row($this->query_id);
  328. return $this->fetch_row;
  329. }
  330.  
  331. /**
  332. * Database::numfields()
  333. *
  334. * @param integer $query_id
  335. * @return
  336. */
  337. function numfields($query_id = -1)
  338. {
  339. if ($query_id != -1)
  340. $this->query_id = $query_id;
  341.  
  342. $this->num_fields = mysql_num_fields($this->query_id);
  343. return $this->num_fields;
  344. }
  345.  
  346. /**
  347. * Database::show()
  348. *
  349. * @return
  350. */
  351. function show()
  352. {
  353. return "<br /><br /><b> Debug Mode - All Queries :</b><hr size='1' /> " . $this->query_show . "<br />";
  354. }
  355.  
  356. /**
  357. * Database::pre()
  358. *
  359. * @return
  360. */
  361. function pre($arr)
  362. {
  363. print '<pre>' . print_r($arr, true) . '</pre>';
  364. }
  365.  
  366.  
  367. /**
  368. * Database::escape()
  369. * @param mixed $string
  370. * @return
  371. */
  372. function escape($string)
  373. {
  374. if (is_array($string)) {
  375. foreach ($string as $key => $value) :
  376. $string[$key] = $this->escape_($value);
  377. endforeach;
  378. } else
  379. $string = $this->escape_($string);
  380.  
  381. return $string;
  382. }
  383.  
  384. /**
  385. * Database::escape_()
  386. *
  387. * @param mixed $string
  388. * @param bool $do
  389. * @return Database::quote()
  390. */
  391. function escape_($string)
  392. {
  393. return mysql_real_escape_string($string, $this->link_id);
  394. }
  395.  
  396. /**
  397. * Database::getDB()
  398. *
  399. * @return
  400. */
  401. function getDB()
  402. {
  403. return $this->database;
  404. }
  405.  
  406. /**
  407. * Database::getServer()
  408. *
  409. * @return
  410. */
  411. function getServer()
  412. {
  413. return $this->server;
  414. }
  415.  
  416. /**
  417. * Database::error()
  418. * Output error message
  419. * @param mixed $msg
  420. * @return
  421. */
  422. function error($msg = '')
  423. {
  424. global $DEBUG, $_SERVER;
  425. if ($this->link_id > 0) {
  426. $this->error_desc = mysql_error($this->link_id);
  427. $this->error_no = mysql_errno($this->link_id);
  428. } else {
  429. $this->error_desc = mysql_error();
  430. $this->error_no = mysql_errno();
  431. }
  432.  
  433. $the_error = "<div style="background-color:#FFF; border: 3px solid #999; padding:10px">";
  434. $the_error .= "<b>mySQL WARNING!</b><br />";
  435. $the_error .= "DB Error: $msg <br /> More Information: <br />";
  436. $the_error .= "<ul>";
  437. $the_error .= "<li> Mysql Error : " . $this->error_no . "</li>";
  438. $the_error .= "<li> Mysql Error no # : " . $this->error_desc . "</li>";
  439. $the_error .= "<li> Date : " . date("F j, Y, g:i a") . "</li>";
  440. $the_error .= "<li> Referer: " . isset($_SERVER['HTTP_REFERER']) . "</li>";
  441. $the_error .= "<li> Script: " . $_SERVER['REQUEST_URI'] . "</li>";
  442. $the_error .= '</ul>';
  443. $the_error .= '</div>';
  444. if ($DEBUG)
  445. echo $the_error;
  446. die();
  447. }
  448.  
  449. }
  450. ?>
  451.  
  452. function connect_db($server, $user, $pass)
  453. {
  454. return mysql_connect($server, $user, $pass);
  455. }
  456.  
  457. private $server = "I put the server info here.";
  458. private $user = "I put the username here.";
  459. private $pass = "I put the password here.";
  460. private $database = "I put the db name here.";
Add Comment
Please, Sign In to add comment