mysql.php | WebReference

mysql.php

mysql.php Source
0  <?
1  
2  Class MySQL
3  {
4  	var $CONN = "";
5  	var $DBASE = "phpHoo";
6  	var $USER = "joe";
7  	var $PASS = "tl0c112";
8  	var $SERVER = "localhost";
9  
10  	var $TRAIL = array();
11  	var $HITS = array();
12  
13  	var $AUTOAPPROVE = true;
14  
15  	function error($text)
16  	{
17  		$no = mysql_errno();
18  		$msg = mysql_error();
19  		echo "[$text] ( $no : $msg )<BR>\n";
20  		exit;
21  	}
22  
23  	function init ()
24  	{
25  		$user = $this->USER;
26  		$pass = $this->PASS;
27  		$server = $this->SERVER;
28  		$dbase = $this->DBASE;
29  
30  		$conn = mysql_connect($server,$user,$pass);
31  		if(!$conn) {
32  			$this->error("Connection attempt failed");
33  		}
34  		if(!mysql_select_db($dbase,$conn)) {
35  			$this->error("Dbase Select failed");
36  		}
37  		$this->CONN = $conn;
38  		return true;
39  	}
40  
41  //	*****************************************************************
42  //						MySQL Specific methods
43  //	*****************************************************************
44  
45  
46  	function select ($sql="", $column="")
47  	{
48  		if(empty($sql)) { return false; }
49  		if(!eregi("^select",$sql))
50  		{
51  			echo "<H2>Wrong function silly!</H2>\n";
52  			return false;
53  		}
54  		if(empty($this->CONN)) { return false; }
55  		$conn = $this->CONN;
56  		$results = mysql_query($sql,$conn);
57  		if( (!$results) or (empty($results)) ) {
58  			mysql_free_result($results);
59  			return false;
60  		}
61  		$count = 0;
62  		$data = array();
63  		while ( $row = mysql_fetch_array($results))
64  		{
65  			$data[$count] = $row;
66  			$count++;
67  		}
68  		mysql_free_result($results);
69  		return $data;
70  	}
71  
72  	function insert ($sql="")
73  	{
74  		if(empty($sql)) { return false; }
75  		if(!eregi("^insert",$sql))
76  		{
77  			echo "<H2>Wrong function silly!</H2>\n";
78  			return false;
79  		}
80  		if(empty($this->CONN)) { return false; }
81  		$conn = $this->CONN;
82  		$results = mysql_query($sql,$conn);
83  		if(!$results) { return false; }
84  		$results = mysql_insert_id();
85  		return $results;
86  	}
87  
88  //	*****************************************************************
89  //						phpHoo Specific Methods
90  //	*****************************************************************
91  
92  	function get_Cats ($CatParent= "")
93  	{
94  		if(empty($CatParent))
95  		{
96  			$CatParent = "IS NULL";
97  		} else {
98  			$CatParent = "= $CatParent";
99  		}
100  		$sql = "SELECT CatID,CatName FROM Categories WHERE CatParent $CatParent ORDER BY CatName";
101  		$results = $this->select($sql);
102  		return $results;
103  	}
104  
105  //	The primer for a recursive query
106  	function get_ParentsInt($CatID="")
107  	{
108  		if(empty($CatID)) { return false; }
109  		unset($this->TRAIL);
110  		$this->TRAIL = array();
111  		$this->get_Parents($CatID);
112  	}
113  
114  //	Use get_ParentsInt(), NOT this one!
115  //	The power of recursive queries
116  
117  	function get_Parents ($CatID="")
118  	{
119  		if( (empty($CatID)) or ("$CatID" == "NULL")) { return false; }
120  		$sql = "SELECT CatID,CatParent,CatName from Categories where CatID = $CatID";
121  
122  		$conn = $this->CONN;
123  		$results = mysql_query($sql,$conn);
124  		if( (!$results) or (empty($results)) ) {
125  			mysql_free_result($results);
126  			return false;
127  		}
128  
129  		while ( $row = mysql_fetch_array($results))
130  		{
131  			$trail = $this->TRAIL;
132  			$count = count($trail);
133  			$trail[$count] = $row;
134  			$this->TRAIL = $trail;
135  			$id = $row["CatParent"];
136  			$this->get_Parents($id);
137  		}
138  		return true;
139  	}
140  
141  	function get_CatIDFromName($CatName="")
142  	{
143  		if(empty($CatName)) { return false; }
144  		$sql = "SELECT CatID from Categories where CatName = '$CatName'";
145  		$results = $this->select($sql);
146  		if(!empty($results))
147  		{
148  			$results = $results[0]["CatID"];
149  		}
150  		return $results;
151  	}
152  
153  	function get_CatNames( $CatID="")
154  	{
155  		if($CatID == 0) { return "Top"; }
156  		$single = false;
157  		if(!empty($CatID))
158  		{
159  			$single = true;
160  			$CatID = "WHERE CatID = $CatID";
161  		}
162  		$sql = "SELECT CatName from Categories $CatID";
163  		$results = $this->select($sql);
164  		if($single)
165  		{
166  			if(!empty($results))
167  			{
168  				$results = $results[0]["CatName"];
169  			}
170  		}
171  		return $results;
172  	}
173  
174  	function get_Links($CatID = "")
175  	{
176  		if(empty($CatID))
177  		{
178  			$CatID = "= 0";
179  		} else {
180  			$CatID = "= $CatID";
181  		}
182  
183  		$sql = "SELECT Url,LinkName,Description FROM Links WHERE  (Approved != 0) AND CatID $CatID ORDER BY Url";
184  		$results = $this->select($sql);
185  		return $results;
186  	}
187  
188  	function get_CatFromLink($LinkID="")
189  	{
190  		if(empty($LinkID)) { return false; }
191  		$sql = "SELECT CatID FROM Links WHERE LinkID = $LinkID";
192  		$results = $this->select($sql);
193  		if(!empty($results))
194  		{
195  			$results = $results[0]["CatID"];
196  		}
197  		return $results;
198  	}
199  
200  	function search ($keywords = "")
201  	{
202  		if(empty($keywords)) { return false; }
203  
204  		$DEBUG = ""; // set DEBUG == "\n" to see this query
205  
206  		$keywords = trim(urldecode($keywords));
207  		$keywords = ereg_replace("([    ]+)"," ",$keywords);
208  
209  		if(!ereg(" ",$keywords))
210  		{
211  			// Only 1 keyword
212  			$KeyWords[0] = "$keywords";
213  		} else {
214  			$KeyWords = explode(" ",$keywords);
215  		}
216  
217  		$sql = "SELECT DISTINCT LinkID,CatID,Url,LinkName,Description FROM Links WHERE (Approved != 0) AND ( $DEBUG ";
218  		$count = count($KeyWords);
219  
220  		if( $count == 1)
221  		{
222  			$single = $KeyWords[0];
223  			$sql .= " (Description LIKE '%$single%') OR (LinkName LIKE '%$single%') OR (Url LIKE '%$single%') ) ORDER BY LinkName $DEBUG ";
224  		} else {
225  			$ticker = 0;
226  			while ( list ($key,$word) = each ($KeyWords) )
227  			{
228  				$ticker++;
229  				if(!empty($word))
230  				{
231  					if($ticker != $count)
232  					{
233  						$sql .= " ( (Description LIKE '%$word%') OR (LinkName LIKE '%$word%') OR (Url LIKE '%$word%') ) OR $DEBUG ";
234  					} else {
235  						// Last condition, omit the trailing OR
236  						$sql .= " ( (Description LIKE '%$word%') OR (LinkName LIKE '%$word%') OR (Url LIKE '%$word%') ) $DEBUG ";
237  					}
238  				}
239  			}
240  			$sql .= " ) ORDER BY LinkName $DEBUG";
241  		}
242  
243  		if(!empty($DEBUG)) { echo "<PRE>$sql\nTicker [$ticker]\nCount [$count]</PRE>\n"; }
244  
245  		$results = $this->select($sql);
246  		return $results;
247  	}
248  
249  	function suggest ($postData="")
250  	{
251  		if( (empty($postData)) or (!is_array($postData)) ) { return false; }
252  
253  		$CatID = $postData["CatID"];
254  		$Url = $postData["Url"];
255  		$Description = $postData["Description"];
256  		$LinkName = $postData["LinkName"];
257  		$SubmitName = $postData["SubmitName"];
258  		$SubmitEmail = $postData["SubmitEmail"];
259  		$SubmitDate = time();
260  
261  		if(empty($Url)) { return false; }
262  		if(empty($Description)) { return false; }
263  		if(empty($LinkName)) { return false; }
264  		if(empty($SubmitName)) { return false; }
265  		if(empty($SubmitEmail)) { return false; }
266  
267  		$Approved = 0;
268  		if($this->AUTOAPPROVE) { $Approved = 1; }
269  
270  
271  		$sql = "INSERT INTO Links ";
272  		$sql .= "(CatID,Url,LinkName,Description,SubmitName,SubmitEmail,SubmitDate,Approved) ";
273  		$sql .= "values ";
274  		$sql .= "($CatID,'$Url','$LinkName','$Description','$SubmitName','$SubmitEmail',$SubmitDate,$Approved) ";
275  		$results = $this->insert($sql);
276  		return $results;
277  	}
278  
279  }	//	End Class
280  ?>