Main Page   File List   File Members   Related Pages  

db_mysql.php

Go to the documentation of this file.
00001 <?php
00002 // $Header: /cvsroot/modphpcms/modphpcms/modules/db_mysql.php,v 1.6 2004/05/04 22:33:17 fred87 Exp $
00003 function create_entry_table($name) // create a key-value table
00004 //{{{
00005 {
00006         $link = mysql_connect(
00007                 config_db_host,
00008                 config_db_user,
00009                 config_db_password
00010                 ) or die("Could not connect to database: " . mysql_error());
00011         
00012         mysql_select_db(config_db_dbname) or
00013                 die("Could not select database: " . mysql_error());
00014 
00015         $query = 'CREATE TABLE `' . config_db_prefix . $name . '` (' .
00016                 '`key` TINYTEXT NOT NULL ,' .
00017                 '`value` LONGTEXT NOT NULL )';
00018         
00019         mysql_query($query);
00020         mysql_close($link);
00021 }
00022 //}}}
00023 function drop_table($name)
00024 //{{{
00025 {
00026         $link = mysql_connect(
00027                 config_db_host,
00028                 config_db_user,
00029                 config_db_password
00030                 ) or die("Could not connect to database: " . mysql_error());
00031         
00032         mysql_select_db(config_db_dbname) or
00033                 die("Could not select database: " . mysql_error());
00034         
00035         $query = 'DROP TABLE `' . config_db_prefix . $name . '`';
00036 
00037         mysql_query($query);
00038         mysql_close($link);
00039 }
00040 //}}}
00041 function delete_entry($table, $key)
00042 //{{{
00043 {
00044         $link = mysql_connect(
00045                 config_db_host,
00046                 config_db_user,
00047                 config_db_password
00048                 ) or die("Could not connect to database: " . mysql_error());
00049         
00050         mysql_select_db(config_db_dbname) or
00051                 die("Could not select database: " . mysql_error());
00052         
00053         $query = 'DELETE FROM `' . config_db_prefix . $table .
00054                 'WHERE `key` = \'' . $key . '\'';
00055         
00056         mysql_query($query);
00057         mysql_close($link);
00058 }
00059 //}}}
00060 function set_entry($table, $key, $value)
00061 //{{{
00062 {
00063         set_field($table, 'key', $key, 'value', $value);
00064 }
00065 //}}}
00066 function get_entry($table, $key)
00067 //{{{
00068 {
00069         return get_field($table, 'key', $key, 'value');
00070 }
00071 //}}}
00072 function get_module_list()
00073 //{{{
00074 {
00075         $link = mysql_connect(
00076                 config_db_host,
00077                 config_db_user,
00078                 config_db_password
00079                 ) or die("Could not connect to database: " . mysql_error());
00080 
00081         mysql_select_db(config_db_dbname) or
00082                 die("Could not select database: " . mysql_error());
00083         
00084         $query = 'SELECT `name` FROM `' . config_db_prefix . 'modules`';
00085         
00086         $result = mysql_query($query) or die("SELECT failed: " . mysql_error());
00087 
00088         $output = '';
00089         
00090         while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
00091         {
00092                 $output .= $row['name'] . ';';
00093         }
00094 
00095         $output = substr("$output", 0, -1); // remove the trailing/unwanted ';'
00096 
00097         mysql_free_result($result);
00098         mysql_close($link);
00099         
00100         return $output;
00101 }
00102 //}}}
00103 function get_module_info($name)
00104 //{{{
00105 {
00106         return get_array('modules', 'name', $name);
00107 }
00108 //}}}
00109 function set_module_info($info)
00110 //{{{
00111 {
00112         $link = mysql_connect(
00113                 config_db_host,
00114                 config_db_user,
00115                 config_db_password
00116                 ) or die("Could not connect to database: " . mysql_error());
00117         
00118         mysql_select_db(config_db_dbname) or
00119                 die("Could not select database: " . mysql_error());
00120         
00121         // First see if the module exists
00122         $query = 'SELECT * FROM `' . config_db_prefix . 'modules` WHERE `name` ' .
00123                 '= \'' . $info['name'] . '\'';
00124                 
00125         $result = mysql_query($query) or die("SELECT failed: " . mysql_error());
00126         $num_rows = mysql_num_rows($result);
00127         mysql_free_result($result);
00128 
00129         if ($num_rows > 0)
00130         {
00131                 // Already exists
00132                 $query = 'UPDATE `' . config_db_prefix . 'modules` SET ' .
00133                         '`enabled` = \'' . $info['enabled'] . '\', ' .
00134                         '`has_admin` = \'' . $info['has_admin'] . '\', ' .
00135                         '`description` = \'' . $info['description'] . '\', ' .
00136                         '`title` = \'' . $info['title'] . '\' ' .
00137                         'WHERE `name` = \'' . $info['name'] . '\'';
00138                 mysql_query($query) or die("UPDATE failed: " . mysql_error());
00139         }
00140         else
00141         {
00142                 // Create the entry
00143                 $query = 'INSERT INTO `' . config_db_prefix . 'modules` '.
00144                         '(`name`,`title`,`enabled`,`has_admin`,`description`) VALUES(' .
00145                         "'" . $info['name'] . "'," .
00146                         "'" . $info['title'] . "'," .
00147                         "'" . $info['enabled'] . "'," .
00148                         "'" . $info['has_admin'] . "'," .
00149                         "'" . $info['description'] .
00150                         "')";
00151                 mysql_query($query) or die("INSERT failed: " . mysql_error());
00152         }
00153         mysql_close($link);
00154 }
00155 //}}}
00156 function delete_module($name)
00157 //{{{
00158 {
00159         $link = mysql_connect(
00160                 config_db_host,
00161                 config_db_user,
00162                 config_db_password
00163                 ) or die("Could not connect to database: " . mysql_error());
00164 
00165         mysql_select_db(config_db_dbname) or
00166                 die("Could not select database: " . mysql_error());
00167 
00168         $query = 'DELETE FROM `' . config_db_prefix . 'modules` WHERE `name` ' .
00169                 ' = \'' . $name . '\'';
00170         mysql_query($query);
00171         mysql_close($link);
00172 
00173         module_installed(); // sorry, misnomer
00174 }
00175 //}}}
00176 function optimize_table($name)
00177 //{{{
00178 {
00179         $link = mysql_connect(
00180                 config_db_host,
00181                 config_db_user,
00182                 config_db_password
00183                 ) or die("Could not connect to database: " . mysql_error());
00184 
00185         mysql_select_db(config_db_dbname) or
00186                 die("Could not select database: " . mysql_error());
00187 
00188         $name = config_db_prefix . $name;
00189         $query = "OPTIMIZE TABLE `$name`";
00190         mysql_query($query);
00191         mysql_close($link);
00192 }
00193 //}}}
00194 function add_field($table, $field, $type)
00195 //{{{
00196 {
00197         $link = mysql_connect(
00198                 config_db_host,
00199                 config_db_user,
00200                 config_db_password
00201                 ) or die("Could not connect to database: " . mysql_error());
00202         
00203         mysql_select_db(config_db_dbname) or
00204                 die("Could not select database: " . mysql_error());
00205         
00206         $table = config_db_prefix . $table;
00207         $query = "ALTER TABLE `$table` ADD `$field` $type";
00208         mysql_query($query);
00209         mysql_close($link);
00210 }
00211 //}}}
00212 function drop_field($table, $field)
00213 //{{{
00214 {
00215     $link = mysql_connect(
00216         config_db_host,
00217         config_db_user,
00218         config_db_password
00219         ) or die("Could not connect to database: " . mysql_error());
00220 
00221     mysql_select_db(config_db_dbname) or
00222         die("Could not select database: " . mysql_error());
00223 
00224     $table = config_db_prefix . $table;
00225     $query = "ALTER TABLE `$table` DROP `$field`";
00226     mysql_query($query);
00227     mysql_close($link);
00228 }
00229 
00230 //}}}
00231 function get_array($table, $searchfield, $searchvalue)
00232 //{{{
00233 {
00234         $link = mysql_connect(
00235                 config_db_host,
00236                 config_db_user,
00237                 config_db_password
00238                 ) or die("Could not connect to database: " . mysql_error());
00239         
00240         mysql_select_db(config_db_dbname) or
00241                 die("Could not select database: " . mysql_error());
00242         
00243         $table = config_db_prefix . $table;
00244         $query = "SELECT * FROM `$table` WHERE `$searchfield` = " .
00245                 "'$searchvalue'";
00246         $result = mysql_query($query) or die("SELECT failed: " . mysql_error());
00247 
00248         $found = mysql_fetch_array($result, MYSQL_ASSOC);
00249 
00250         mysql_free_result($result);
00251         mysql_close($link);
00252         return $found;
00253 }               
00254 //}}}
00255 function set_field($table, $searchfield, $searchvalue, $field, $value)
00256 //{{{
00257 {
00258         $link = mysql_connect(
00259                 config_db_host,
00260                 config_db_user,
00261                 config_db_password
00262                 ) or die("Could not connect to database: " . mysql_error());
00263         mysql_select_db(config_db_dbname) or
00264                 die("Could not select database: " . mysql_error());
00265 
00266         $table = config_db_prefix . $table;
00267         
00268         // First see if the entry exists
00269         $query = "SELECT `$field` FROM `$table` WHERE `$searchfield` = '$searchvalue'";
00270         $result = mysql_query($query) or die("SELECT failed: " . mysql_error());
00271         $numrows = mysql_num_rows($result);
00272         mysql_free_result($result);
00273 
00274         if ($numrows > 0)
00275         {
00276                 // Already exists
00277                 $query = "UPDATE `$table` SET `$field` = '$value' WHERE " .
00278                         "`$searchfield` = '$searchvalue'";
00279                 mysql_query($query) or die("UPDATE failed: " . mysql_error());
00280         }
00281         else
00282         {
00283                 // Create the entry
00284                 $query = "INSERT INTO `$table` " .
00285                         "(`$searchfield`, `$field`) VALUES " .
00286                         "('$searchvalue', '$value')";
00287                 mysql_query($query) or die("INSERT failed: " . mysql_error());
00288         }
00289         mysql_close($link);
00290 }
00291 //}}}
00292 function get_field($table, $searchfield, $searchvalue, $field)
00293 //{{{
00294 {
00295         $found = get_array($table, $searchfield, $searchvalue);
00296         return $found[$field];
00297 }
00298 //}}}
00299 function get_reccount($table)
00300 //{{{
00301 {
00302         $link = mysql_connect(
00303                 config_db_host,
00304                 config_db_user,
00305                 config_db_password
00306                 ) or die("Could not connect to database: " . mysql_error());
00307         mysql_select_db(config_db_dbname) or
00308                 die("Could not select database: " . mysql_error());
00309         
00310         $table = config_db_prefix . $table;
00311 
00312         $query = "SELECT * FROM `$table`";
00313         $result = mysql_query($query) or die("SELECT failed: " . mysql_error());
00314         $numrows = mysql_num_rows($result);
00315         mysql_free_result($result);
00316         mysql_close($link);
00317         return $numrows;
00318 }
00319 //}}}
00320 function get_array2($table, $recnum)
00321 //{{{
00322 {
00323         $link = mysql_connect(
00324                 config_db_host,
00325                 config_db_user,
00326                 config_db_password
00327                 ) or die("Could not connect to database: " . mysql_error());
00328         
00329         mysql_select_db(config_db_dbname) or
00330                 die("Could not select database: " . mysql_error());
00331         
00332         $table = config_db_prefix . $table;
00333         $query = "SELECT * FROM `$table`";
00334         $result = mysql_query($query) or die("SELECT failed: " . mysql_error());
00335         
00336         $i = 0;
00337         while ($found = mysql_fetch_assoc($result))
00338         {
00339                 $i++;
00340                 if ($i == $recnum)
00341                         break;
00342         }
00343 
00344         mysql_free_result($result);
00345         mysql_close($link);
00346         return $found;
00347 }
00348 //}}}
00349 function get_field2($table, $field, $recnum)
00350 //{{{
00351 {
00352         $found = get_array2($table, $recnum);
00353         return $found[$field];
00354 }
00355 //}}}
00356 ?>

Generated on Wed Jun 9 03:38:16 2004 for modphpcms by doxygen1.2.18