01 <?php
02
03 class simple_mySQL_class
04 {
05 var $db;
06
07 function simple_mySQL_class($dbname, $username, $password)
08 {
09 // change "localhost" to your database-server address
10 // if your database-server is not the machine running this script
11 $this->db = mysql_connect('localhost', $username, $password)
12 or die('Unable to connect to Database Server');
13
14 mysql_select_db($dbname, $this->db)
15 or die('Could not select database');
16 }
17
18 function query($sql)
19 {
20 $result = mysql_query($sql, $this->db)
21 or die('Invalid query: '.mysql_error());
22 return $result;
23 }
24
25 function fetch($sql)
26 {
27 $data = array();
28 $result = $this->query($sql);
29 while($row = mysql_fetch_assoc($result))
30 {
31 $data[] = $row;
32 }
33 return $data;
34 }
35
36 function getone($sql)
37 {
38 $result = $this->query($sql);
39 if(mysql_num_rows($result) == 0)
40 $value = FALSE;
41 else
42 $value = mysql_result($result, 0);
43 return $value;
44 }
45 }
46
47 // use like
48 // connect to the database with database name, username and password
49 $dbconnect = new simple_mySQL_class('database', 'user', 'password');
50 // build your mySQL query
51 $query = 'SELECT * FROM `mytable` WHERE `id`=\''.$search.'\' ORDER BY `date` DESC';
52 // get the result in an array
53 $result = $dbconnect->fetch($query);
54
55 // output the data in whatever way you want...
56 echo '<pre>';
57 print_r($result);
58 echo '</pre>';
59
60 ?>