| [ Index ] |
PHP Cross Reference of Crawler |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Custom library of bare-bones MYSQL query and minipulation functions to peform basic SELECT, INSERT, and UPDATE queries. 4 * 5 * Library provides a VERY lightweight wrapper to to sanitize data and run simple MySQL queries. Accepts associative arrays for both 6 * query and data, and return associative arrays with results. 7 * 8 * Functions assume a MySQL connection has been established, and that a database has been selected, e.g. 9 * <code> 10 * $db=mysql_connect (MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD) or die ('I cannot connect to the database because: ' . mysql_error()); 11 * mysql_select_db (MYSQL_DATABASE); 12 * </code> 13 * 14 * @author Benjamin J. Balter <ben@balter.com> 15 * @package mysql_functions 16 * @version 1.1 17 * 18 */ 19 20 /** 21 * Function to generate a multi-dimensional associative array from a MySQL resource. 22 * 23 * Example Usage, given the table below 24 * 25 * Players 26 * -------------------- 27 * ID Name Position 28 * -------------------- 29 * 1 Kevin 1B 30 * 2 Tom LF 31 * 3 Sally SS 32 * 33 * The Command "mysql_array(mysql_select('players'))" 34 * 35 * Would return the following array 36 * <code> 37 * array( 38 * [1] => array( 39 * ['ID'] => 1, 40 * ['Name'] => "Kevin", 41 * ['Position'] => '1B' 42 * ), 43 * [2] => array( 44 * ['ID'] => 2, 45 * ['Name'] => "Tom", 46 * ['Position'] => 'LF' 47 * ), 48 * 49 * [3] => array( 50 * ['ID'] => 3, 51 * ['Name'] => "Sally", 52 * ['Position'] => 'SS' 53 * ) 54 * ) 55 * 56 * @param resource $result MySQL resource object (either output of mysql_query($sql) or mysql_select('table',$query)) 57 * @param bool $assoc makes Associate array optional (added 1.1) 58 * @return array Multi-dimensional Associative array keyed to first field in table, returns empty array if no results 59 * 60 */ 61 function mysql_array($result,$assoc = TRUE) { 62 63 //Start with a null results set 64 $results = array(); 65 66 if ($assoc) { 67 //Grab the first fieldname to key the array with 68 $first = mysql_field_name($result,0); 69 70 //Loop through each row and build an assoc. array 71 while ($row = mysql_fetch_assoc($result)) $results += array($row[$first] => $row); 72 } else { 73 //Loop through each row and build a array 74 while ($row = mysql_fetch_assoc($result)) $results[] = $row; 75 } 76 77 //Strip slashes and return 78 return stripslashes_deep($results); 79 } 80 81 /** 82 * Returns an array of a single MySQL result row. 83 * 84 * Similar to mysql_fetch_assoc exccept it strips slashes, returns an empty array (rather than an error) if resource is bad or no results are found 85 * 86 * @param resource $result MySQL resource object (either output of mysql_query($sql) or mysql_select('table',$query)) 87 * @return array Associative array of row, returns empty array if no results 88 * @package mysql_functions 89 */ 90 function mysql_row_array($result) { 91 92 // Verify we have a valid MySQL resource, otherwise return an empty array 93 if (!$result) return array(); 94 95 // Veryify there are results to the query, otherwise return an empty array 96 if (mysql_num_rows($result) ==0) return array(); 97 98 //Strip slashes and return the result of mysql_fetch_assoc. 99 return stripslashes_deep(mysql_fetch_assoc($result)); 100 } 101 102 /** 103 * Generates SQL query, sanitizes data, and inserts row into database. 104 * 105 * Example Usage 106 * <code> 107 * $data = array( 'Name'=>'Joan', 108 * 'Position'=>'2B' 109 * ); 110 * mysql_insert('players',$data); 111 * </code> 112 * 113 * @param string $table Name of table to operate on 114 * @param array $data Associative array of data fields and values 115 * @returns int|bool ID of inserted row if valid, false if invalid 116 * @package mysql_functions 117 */ 118 119 function mysql_insert($table, $data) { 120 121 //Build query 122 $sql = "INSERT INTO `$table` ("; 123 foreach ($data as $field => $value) $sql .= "`$field`, "; 124 $sql = substr($sql,0,strlen($sql)-2) . ") VALUES ("; 125 foreach ($data as $field => $value) $sql .= "'" . addslashes($value) . "', "; 126 127 //Remove last comma 128 $sql = substr($sql,0,strlen($sql)-2) . ")"; 129 130 // Run query and return either ID or false (error) 131 if (mysql_query($sql)) return mysql_insert_id(); 132 return false; 133 } 134 135 /** 136 * Generates SQL query, sanitizes data, and updates row in database. 137 * 138 * Example Usage 139 * <code> 140 * //Updates Tom's row and moves him to Right Field 141 * $data = array( 'Position' => 'RF'); 142 * $query = array( 'Name' => 'Tom' ); 143 * mysql_update('players', $data, $query); 144 * 145 * ---- 146 * 147 * //Updates all players named 'Tom' or in Left Field and moves them to Right Field 148 * $data = array( 'Position' => 'RF'); 149 * $query = array( 'Name' => 'Tom', 'Position' => 'LF' ); 150 * mysql_update('players', $data, $query, "OR"); 151 * </code> 152 * 153 * @param string $table Name of table to operate on 154 * @param array $data Associative array of data fields and values 155 * @param array $query Associative array of query fields and values 156 * @param string $connector (Optional) connector for quiery ('AND' or 'OR') 157 * @return bool true or false on sucess or fail 158 * @package mysql_functions 159 */ 160 161 function mysql_update($table, $data, $query, $connector = "AND") { 162 163 //Format the SQL query 164 $sql = "UPDATE `$table` SET "; 165 foreach ($data as $field => $value) $sql .= "`$field` = '" . addslashes($value) ."', "; 166 $sql = substr($sql,0,strlen($sql)-2) . " WHERE "; 167 foreach ($query as $field => $value) $sql .= "`$field` = '$value' $connector "; 168 169 //Remove the last connector 170 $sql = substr($sql,0,strlen($sql)-(strlen($connector)+1)); 171 172 //return a bool with the query's result 173 if (mysql_query($sql)) return true; 174 return false; 175 } 176 /** 177 * Builds an SQL query, sanitizes the data, removes a row from the database. 178 * 179 * EXAMPLE USAGE 180 * <code> 181 * $query = array('ID'=>'3'); 182 * mysql_remove('players',$query); 183 * </code> 184 * 185 * @param string $table Name of table to operate on 186 * @param array $query Associative array of query field names and values 187 * @param string $connector (Optional) query connector ('AND' or 'OR') 188 * @return bool true or false for sucess or fail 189 * @package mysql_functions 190 * 191 */ 192 function mysql_remove($table, $query=array(), $connector = "AND") { 193 194 //Build the SQL Query 195 $sql = "DELETE FROM `$table` WHERE "; 196 foreach ($query as $field => $value) $sql .= "`$field` = '" . addslashes($value) . "' $connector "; 197 198 //Remove the last connecter 199 $sql = substr($sql,0,strlen($sql)-(strlen($connector)+1)); 200 201 //return a bool with the query's result 202 if (mysql_query($sql)) return true; 203 return false; 204 } 205 206 /** 207 * Builds an SQL query, sanatizes data, and return a MySQL resource object with the results. 208 * 209 * Typically used in conjunction with mysql_array or mysql_row_array to handle simple MySQL queries 210 * 211 * For example, to return an entire table: 212 * <code> 213 * mysql_select('Players'); 214 * </code> 215 * Or to return a select set of results: 216 * <code> 217 * $query = array('Name'=>'Tom'); 218 * mysql_select('Players',$query); 219 * </code> 220 * 221 * @param string $table Name of table to operate on 222 * @param array $query Associative array of query field names and values 223 * @param string $connector (Optional) query connector ('AND' or 'OR') 224 * @return object MySQL resource object with results 225 * @package mysql_functions 226 * 227 */ 228 function mysql_select($table, $query=array(), $connector = "AND") { 229 230 //Build the SQL Query 231 $sql = "SELECT * FROM `$table` "; 232 233 //If there is no WHERE clause, just run the query 234 if (sizeof($query)>0) { 235 $sql .= "WHERE "; 236 237 //Loop through the fields/values 238 foreach ($query as $field => $value) $sql .= "`$field` = '" . addslashes($value) . "' $connector "; 239 240 //Remove the last connector 241 $sql = substr($sql,0,strlen($sql)-(strlen($connector)+1)); 242 } 243 244 //Run the query 245 $result = mysql_query($sql); 246 247 //Output an errors if applicable 248 if (mysql_error()) echo "<p>" . mysql_error() . ": $sql</p>"; 249 250 //Return the result (as a MySQL resource) 251 return $result; 252 } 253 254 /** 255 * Runs a simple mysql SELECT query and returns true or false if results are found. 256 * 257 * Used to verify data (such as a username or password) when the existence of the fields (rather than their value) is what is sought 258 * 259 * @param string $table Name of table to operate on 260 * @param array $query Associative array of query field names and values 261 * @param string $connector (Optional) query connector ('AND' or 'OR') 262 * @return bool returns true if one or more results found, otherwise returns false 263 * @package mysql_functions 264 * 265 */ 266 function mysql_exists($table,$query=array(),$connector="AND") { 267 $result = mysql_select($table,$query,$connector); 268 if (mysql_num_rows($result)!=0) return true; 269 return false; 270 } 271 272 /** 273 * Removes slashes from multi-dimensional arrays. 274 * 275 * Runs stripslashes() on all values in a multi-dimensial array. Used with mysql_array to remove slashes added by add_slashes() form mysql_insert(). 276 * Can also accept a standard array. 277 * 278 * @param array $value Array to be sanitized, may be single or multi-dimensional 279 * @return array Return array identical to one given but with slashes removed 280 * @package mysql_functions 281 * 282 */ 283 function stripslashes_deep($value) { 284 $value = is_array($value) ? 285 array_map('stripslashes_deep', $value) : 286 stripslashes($value); 287 return $value; 288 } 289 290 291 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jun 3 17:10:09 2010 | Cross-referenced by PHPXref 0.7 |