Guest User

class.php

a guest
Sep 24th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 35.40 KB | None | 0 0
  1. <?php
  2.  
  3.         /**********************************************************************
  4.         *  Author: Justin Vincent (jv@vip.ie)
  5.         *  Web...: http://justinvincent.com
  6.         *  Name..: ezSQL
  7.         *  Desc..: ezSQL Core module - database abstraction library to make
  8.         *          it very easy to deal with databases. ezSQLcore can not be used by
  9.         *          itself (it is designed for use by database specific modules).
  10.         *
  11.         */
  12.  
  13.         /**********************************************************************
  14.         *  ezSQL Constants
  15.         */
  16.  
  17.         define('EZSQL_VERSION','2.08');
  18.         define('OBJECT','OBJECT',true);
  19.         define('ARRAY_A','ARRAY_A',true);
  20.         define('ARRAY_N','ARRAY_N',true);
  21.  
  22.         /**********************************************************************
  23.         *  Core class containg common functions to manipulate query result
  24.         *  sets once returned
  25.         */
  26.  
  27.         class ezSQLcore
  28.         {
  29.  
  30.                 var $trace            = false;  // same as $debug_all
  31.                 var $debug_all        = false;  // same as $trace
  32.                 var $debug_called     = false;
  33.                 var $vardump_called   = false;
  34.                 var $show_errors      = true;
  35.                 var $num_queries      = 0;
  36.                 var $last_query       = null;
  37.                 var $last_error       = null;
  38.                 var $col_info         = null;
  39.                 var $captured_errors  = array();
  40.                 var $cache_dir        = false;
  41.                 var $cache_queries    = false;
  42.                 var $cache_inserts    = false;
  43.                 var $use_disk_cache   = false;
  44.                 var $cache_timeout    = 24; // hours
  45.                 var $timers           = array();
  46.                 var $total_query_time = 0;
  47.                 var $db_connect_time  = 0;
  48.                 var $trace_log        = array();
  49.                 var $use_trace_log    = false;
  50.                 var $sql_log_file     = false;
  51.                 var $do_profile       = false;
  52.                 var $profile_times    = array();
  53.  
  54.                 // == TJH == default now needed for echo of debug function
  55.                 var $debug_echo_is_on = true;
  56.  
  57.                 /**********************************************************************
  58.                 *  Constructor
  59.                 */
  60.  
  61.                 function ezSQLcore()
  62.                 {
  63.                 }
  64.  
  65.                 /**********************************************************************
  66.                 *  Print SQL/DB error - over-ridden by specific DB class
  67.                 */
  68.  
  69.                 function register_error($err_str)
  70.                 {
  71.                         // Keep track of last error
  72.                         $this->last_error = $err_str;
  73.  
  74.                         // Capture all errors to an error array no matter what happens
  75.                         $this->captured_errors[] = array
  76.                         (
  77.                                 'error_str' => $err_str,
  78.                                 'query'     => $this->last_query
  79.                         );
  80.                 }
  81.  
  82.                 /**********************************************************************
  83.                 *  Turn error handling on or off..
  84.                 */
  85.  
  86.                 function show_errors()
  87.                 {
  88.                         $this->show_errors = true;
  89.                 }
  90.  
  91.                 function hide_errors()
  92.                 {
  93.                         $this->show_errors = false;
  94.                 }
  95.  
  96.                 /**********************************************************************
  97.                 *  Kill cached query results
  98.                 */
  99.  
  100.                 function flush()
  101.                 {
  102.                         // Get rid of these
  103.                         $this->last_result = null;
  104.                         $this->col_info = null;
  105.                         $this->last_query = null;
  106.                         $this->from_disk_cache = false;
  107.                 }
  108.  
  109.                 /**********************************************************************
  110.                 *  Get one variable from the DB - see docs for more detail
  111.                 */
  112.  
  113.                 function get_var($query=null,$x=0,$y=0)
  114.                 {
  115.  
  116.                         // Log how the function was called
  117.                         $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  118.  
  119.                         // If there is a query then perform it if not then use cached results..
  120.                         if ( $query )
  121.                         {
  122.                                 $this->query($query);
  123.                         }
  124.  
  125.                         // Extract var out of cached results based x,y vals
  126.                         if ( $this->last_result[$y] )
  127.                         {
  128.                                 $values = array_values(get_object_vars($this->last_result[$y]));
  129.                         }
  130.  
  131.                         // If there is a value return it else return null
  132.                         return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  133.                 }
  134.  
  135.                 /**********************************************************************
  136.                 *  Get one row from the DB - see docs for more detail
  137.                 */
  138.  
  139.                 function get_row($query=null,$output=OBJECT,$y=0)
  140.                 {
  141.  
  142.                         // Log how the function was called
  143.                         $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  144.  
  145.                         // If there is a query then perform it if not then use cached results..
  146.                         if ( $query )
  147.                         {
  148.                                 $this->query($query);
  149.                         }
  150.  
  151.                         // If the output is an object then return object using the row offset..
  152.                         if ( $output == OBJECT )
  153.                         {
  154.                                 return $this->last_result[$y]?$this->last_result[$y]:null;
  155.                         }
  156.                         // If the output is an associative array then return row as such..
  157.                         elseif ( $output == ARRAY_A )
  158.                         {
  159.                                 return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  160.                         }
  161.                         // If the output is an numerical array then return row as such..
  162.                         elseif ( $output == ARRAY_N )
  163.                         {
  164.                                 return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  165.                         }
  166.                         // If invalid output type was specified..
  167.                         else
  168.                         {
  169.                                 $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  170.                         }
  171.  
  172.                 }
  173.  
  174.                 /**********************************************************************
  175.                 *  Function to get 1 column from the cached result set based in X index
  176.                 *  see docs for usage and info
  177.                 */
  178.  
  179.                 function get_col($query=null,$x=0)
  180.                 {
  181.  
  182.                         $new_array = array();
  183.  
  184.                         // If there is a query then perform it if not then use cached results..
  185.                         if ( $query )
  186.                         {
  187.                                 $this->query($query);
  188.                         }
  189.  
  190.                         // Extract the column values
  191.                         for ( $i=0; $i < count($this->last_result); $i++ )
  192.                         {
  193.                                 $new_array[$i] = $this->get_var(null,$x,$i);
  194.                         }
  195.  
  196.                         return $new_array;
  197.                 }
  198.  
  199.  
  200.                 /**********************************************************************
  201.                 *  Return the the query as a result set - see docs for more details
  202.                 */
  203.  
  204.                 function get_results($query=null, $output = OBJECT)
  205.                 {
  206.  
  207.                         // Log how the function was called
  208.                         $this->func_call = "\$db->get_results(\"$query\", $output)";
  209.  
  210.                         // If there is a query then perform it if not then use cached results..
  211.                         if ( $query )
  212.                         {
  213.                                 $this->query($query);
  214.                         }
  215.  
  216.                         // Send back array of objects. Each row is an object
  217.                         if ( $output == OBJECT )
  218.                         {
  219.                                 return $this->last_result;
  220.                         }
  221.                         elseif ( $output == ARRAY_A || $output == ARRAY_N )
  222.                         {
  223.                                 if ( $this->last_result )
  224.                                 {
  225.                                         $i=0;
  226.                                         foreach( $this->last_result as $row )
  227.                                         {
  228.  
  229.                                                 $new_array[$i] = get_object_vars($row);
  230.  
  231.                                                 if ( $output == ARRAY_N )
  232.                                                 {
  233.                                                         $new_array[$i] = array_values($new_array[$i]);
  234.                                                 }
  235.  
  236.                                                 $i++;
  237.                                         }
  238.  
  239.                                         return $new_array;
  240.                                 }
  241.                                 else
  242.                                 {
  243.                                         return null;
  244.                                 }
  245.                         }
  246.                 }
  247.  
  248.  
  249.                 /**********************************************************************
  250.                 *  Function to get column meta data info pertaining to the last query
  251.                 * see docs for more info and usage
  252.                 */
  253.  
  254.                 function get_col_info($info_type="name",$col_offset=-1)
  255.                 {
  256.  
  257.                         if ( $this->col_info )
  258.                         {
  259.                                 if ( $col_offset == -1 )
  260.                                 {
  261.                                         $i=0;
  262.                                         foreach($this->col_info as $col )
  263.                                         {
  264.                                                 $new_array[$i] = $col->{$info_type};
  265.                                                 $i++;
  266.                                         }
  267.                                         return $new_array;
  268.                                 }
  269.                                 else
  270.                                 {
  271.                                         return $this->col_info[$col_offset]->{$info_type};
  272.                                 }
  273.  
  274.                         }
  275.  
  276.                 }
  277.  
  278.                 /**********************************************************************
  279.                 *  store_cache
  280.                 */
  281.  
  282.                 function store_cache($query,$is_insert)
  283.                 {
  284.  
  285.                         // The would be cache file for this query
  286.                         $cache_file = $this->cache_dir.'/'.md5($query);
  287.  
  288.                         // disk caching of queries
  289.                         if ( $this->use_disk_cache && ( $this->cache_queries && ! $is_insert ) || ( $this->cache_inserts && $is_insert ))
  290.                         {
  291.                                 if ( ! is_dir($this->cache_dir) )
  292.                                 {
  293.                                         $this->register_error("Could not open cache dir: $this->cache_dir");
  294.                                         $this->show_errors ? trigger_error("Could not open cache dir: $this->cache_dir",E_USER_WARNING) : null;
  295.                                 }
  296.                                 else
  297.                                 {
  298.                                         // Cache all result values
  299.                                         $result_cache = array
  300.                                         (
  301.                                                 'col_info' => $this->col_info,
  302.                                                 'last_result' => $this->last_result,
  303.                                                 'num_rows' => $this->num_rows,
  304.                                                 'return_value' => $this->num_rows,
  305.                                         );
  306.                                         error_log ( serialize($result_cache), 3, $cache_file);
  307.                                 }
  308.                         }
  309.  
  310.                 }
  311.  
  312.                 /**********************************************************************
  313.                 *  get_cache
  314.                 */
  315.  
  316.                 function get_cache($query)
  317.                 {
  318.  
  319.                         // The would be cache file for this query
  320.                         $cache_file = $this->cache_dir.'/'.md5($query);
  321.  
  322.                         // Try to get previously cached version
  323.                         if ( $this->use_disk_cache && file_exists($cache_file) )
  324.                         {
  325.                                 // Only use this cache file if less than 'cache_timeout' (hours)
  326.                                 if ( (time() - filemtime($cache_file)) > ($this->cache_timeout*3600) )
  327.                                 {
  328.                                         unlink($cache_file);
  329.                                 }
  330.                                 else
  331.                                 {
  332.                                         $result_cache = unserialize(file_get_contents($cache_file));
  333.  
  334.                                         $this->col_info = $result_cache['col_info'];
  335.                                         $this->last_result = $result_cache['last_result'];
  336.                                         $this->num_rows = $result_cache['num_rows'];
  337.  
  338.                                         $this->from_disk_cache = true;
  339.  
  340.                                         // If debug ALL queries
  341.                                         $this->trace || $this->debug_all ? $this->debug() : null ;
  342.  
  343.                                         return $result_cache['return_value'];
  344.                                 }
  345.                         }
  346.  
  347.                 }
  348.  
  349.                 /**********************************************************************
  350.                 *  Dumps the contents of any input variable to screen in a nicely
  351.                 *  formatted and easy to understand way - any type: Object, Var or Array
  352.                 */
  353.  
  354.                 function vardump($mixed='')
  355.                 {
  356.  
  357.                         // Start outup buffering
  358.                         ob_start();
  359.  
  360.                         echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  361.                         echo "<pre><font face=arial>";
  362.  
  363.                         if ( ! $this->vardump_called )
  364.                         {
  365.                                 echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  366.                         }
  367.  
  368.                         $var_type = gettype ($mixed);
  369.                         print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
  370.                         echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  371.                         echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  372.                         echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  373.                         echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  374.                         echo "</font></pre></font></blockquote></td></tr></table>".$this->donation();
  375.                         echo "\n<hr size=1 noshade color=dddddd>";
  376.  
  377.                         // Stop output buffering and capture debug HTML
  378.                         $html = ob_get_contents();
  379.                         ob_end_clean();
  380.  
  381.                         // Only echo output if it is turned on
  382.                         if ( $this->debug_echo_is_on )
  383.                         {
  384.                                 echo $html;
  385.                         }
  386.  
  387.                         $this->vardump_called = true;
  388.  
  389.                         return $html;
  390.  
  391.                 }
  392.  
  393.                 /**********************************************************************
  394.                 *  Alias for the above function
  395.                 */
  396.  
  397.                 function dumpvar($mixed)
  398.                 {
  399.                         $this->vardump($mixed);
  400.                 }
  401.  
  402.                 /**********************************************************************
  403.                 *  Displays the last query string that was sent to the database & a
  404.                 * table listing results (if there were any).
  405.                 * (abstracted into a seperate file to save server overhead).
  406.                 */
  407.  
  408.                 function debug($print_to_screen=true)
  409.                 {
  410.  
  411.                         // Start outup buffering
  412.                         ob_start();
  413.  
  414.                         echo "<blockquote>";
  415.  
  416.                         // Only show ezSQL credits once..
  417.                         if ( ! $this->debug_called )
  418.                         {
  419.                                 echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  420.                         }
  421.  
  422.                         if ( $this->last_error )
  423.                         {
  424.                                 echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->last_error</b></font>]<p>";
  425.                         }
  426.  
  427.                         if ( $this->from_disk_cache )
  428.                         {
  429.                                 echo "<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>";
  430.                         }
  431.  
  432.                         echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  433.                         echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  434.  
  435.                                 echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  436.                                 echo "<blockquote>";
  437.  
  438.                         if ( $this->col_info )
  439.                         {
  440.  
  441.                                 // =====================================================
  442.                                 // Results top rows
  443.  
  444.                                 echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  445.                                 echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  446.  
  447.  
  448.                                 for ( $i=0; $i < count($this->col_info); $i++ )
  449.                                 {
  450.                                         echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>{$this->col_info[$i]->name}</span></td>";
  451.                                 }
  452.  
  453.                                 echo "</tr>";
  454.  
  455.                                 // ======================================================
  456.                                 // print main results
  457.  
  458.                         if ( $this->last_result )
  459.                         {
  460.  
  461.                                 $i=0;
  462.                                 foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  463.                                 {
  464.                                         $i++;
  465.                                         echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  466.  
  467.                                         foreach ( $one_row as $item )
  468.                                         {
  469.                                                 echo "<td nowrap><font face=arial size=2>$item</font></td>";
  470.                                         }
  471.  
  472.                                         echo "</tr>";
  473.                                 }
  474.  
  475.                         } // if last result
  476.                         else
  477.                         {
  478.                                 echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  479.                         }
  480.  
  481.                         echo "</table>";
  482.  
  483.                         } // if col_info
  484.                         else
  485.                         {
  486.                                 echo "<font face=arial size=2>No Results</font>";
  487.                         }
  488.  
  489.                         echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
  490.  
  491.                         // Stop output buffering and capture debug HTML
  492.                         $html = ob_get_contents();
  493.                         ob_end_clean();
  494.  
  495.                         // Only echo output if it is turned on
  496.                         if ( $this->debug_echo_is_on && $print_to_screen)
  497.                         {
  498.                                 echo $html;
  499.                         }
  500.  
  501.                         $this->debug_called = true;
  502.  
  503.                         return $html;
  504.  
  505.                 }
  506.  
  507.                 /**********************************************************************
  508.                 *  Naughty little function to ask for some remuniration!
  509.                 */
  510.  
  511.                 function donation()
  512.                 {
  513.                         return "<font size=1 face=arial color=000000>If ezSQL has helped <a href=\"https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezSQL&no_note=1&tax=0\" style=\"color: 0000CC;\">make a donation!?</a> &nbsp;&nbsp;<!--[ go on! you know you want to! ]--></font>";
  514.                 }
  515.  
  516.                 /**********************************************************************
  517.                 *  Timer related functions
  518.                 */
  519.  
  520.                 function timer_get_cur()
  521.                 {
  522.                         list($usec, $sec) = explode(" ",microtime());
  523.                         return ((float)$usec + (float)$sec);
  524.                 }
  525.  
  526.                 function timer_start($timer_name)
  527.                 {
  528.                         $this->timers[$timer_name] = $this->timer_get_cur();
  529.                 }
  530.  
  531.                 function timer_elapsed($timer_name)
  532.                 {
  533.                         return round($this->timer_get_cur() - $this->timers[$timer_name],2);
  534.                 }
  535.  
  536.                 function timer_update_global($timer_name)
  537.                 {
  538.                         if ( $this->do_profile )
  539.                         {
  540.                                 $this->profile_times[] = array
  541.                                 (
  542.                                         'query' => $this->last_query,
  543.                                         'time' => $this->timer_elapsed($timer_name)
  544.                                 );
  545.                         }
  546.                        
  547.                         $this->total_query_time += $this->timer_elapsed($timer_name);
  548.                 }
  549.  
  550.         }
  551.         /**********************************************************************
  552.         *  Author: Justin Vincent (jv@jvmultimedia.com)
  553.         *  Web...: http://twitter.com/justinvincent
  554.         *  Name..: ezSQL_mysql
  555.         *  Desc..: mySQL component (part of ezSQL databse abstraction library)
  556.         *
  557.         */
  558.  
  559.         /**********************************************************************
  560.         *  ezSQL error strings - mySQL
  561.         */
  562.  
  563.         $ezsql_mysql_str = array
  564.         (
  565.                 1 => 'Require $dbuser and $dbpassword to connect to a database server',
  566.                 2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
  567.                 3 => 'Require $dbname to select a database',
  568.                 4 => 'mySQL database connection is not active',
  569.                 5 => 'Unexpected error while trying to select database'
  570.         );
  571.  
  572.         /**********************************************************************
  573.         *  ezSQL Database specific class - mySQL
  574.         */
  575.  
  576.         if ( ! function_exists ('mysql_connect') ) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
  577.         if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  578.  
  579.         class ezSQL_mysql extends ezSQLcore
  580.         {
  581.  
  582.                 var $dbuser = false;
  583.                 var $dbpassword = false;
  584.                 var $dbname = false;
  585.                 var $dbhost = false;
  586.  
  587.                 /**********************************************************************
  588.                 *  Constructor - allow the user to perform a qucik connect at the
  589.                 *  same time as initialising the ezSQL_mysql class
  590.                 */
  591.  
  592.                 function ezSQL_mysql($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  593.                 {
  594.                         $this->dbuser = $dbuser;
  595.                         $this->dbpassword = $dbpassword;
  596.                         $this->dbname = $dbname;
  597.                         $this->dbhost = $dbhost;
  598.                 }
  599.  
  600.                 /**********************************************************************
  601.                 *  Short hand way to connect to mySQL database server
  602.                 *  and select a mySQL database at the same time
  603.                 */
  604.  
  605.                 function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  606.                 {
  607.                         $return_val = false;
  608.                         if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
  609.                         else if ( ! $this->select($dbname) ) ;
  610.                         else $return_val = true;
  611.                         return $return_val;
  612.                 }
  613.  
  614.                 /**********************************************************************
  615.                 *  Try to connect to mySQL database server
  616.                 */
  617.  
  618.                 function connect($dbuser='', $dbpassword='', $dbhost='localhost')
  619.                 {
  620.                         global $ezsql_mysql_str; $return_val = false;
  621.  
  622.                         // Must have a user and a password
  623.                         if ( ! $dbuser )
  624.                         {
  625.                                 $this->register_error($ezsql_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
  626.                                 $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
  627.                         }
  628.                         // Try to establish the server database handle
  629.                         else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true) )
  630.                         {
  631.                                 $this->register_error($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
  632.                                 $this->show_errors ? trigger_error($ezsql_mysql_str[2],E_USER_WARNING) : null;
  633.                         }
  634.                         else
  635.                         {
  636.                                 $this->dbuser = $dbuser;
  637.                                 $this->dbpassword = $dbpassword;
  638.                                 $this->dbhost = $dbhost;
  639.                                 $return_val = true;
  640.                         }
  641.  
  642.                         return $return_val;
  643.                 }
  644.  
  645.                 /**********************************************************************
  646.                 *  Try to select a mySQL database
  647.                 */
  648.  
  649.                 function select($dbname='')
  650.                 {
  651.                         global $ezsql_mysql_str; $return_val = false;
  652.  
  653.                         // Must have a database name
  654.                         if ( ! $dbname )
  655.                         {
  656.                                 $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
  657.                                 $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
  658.                         }
  659.  
  660.                         // Must have an active database connection
  661.                         else if ( ! $this->dbh )
  662.                         {
  663.                                 $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
  664.                                 $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
  665.                         }
  666.  
  667.                         // Try to connect to the database
  668.                         else if ( !@mysql_select_db($dbname,$this->dbh) )
  669.                         {
  670.                                 // Try to get error supplied by mysql if not use our own
  671.                                 if ( !$str = @mysql_error($this->dbh))
  672.                                           $str = $ezsql_mysql_str[5];
  673.  
  674.                                 $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
  675.                                 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  676.                         }
  677.                         else
  678.                         {
  679.                                 $this->dbname = $dbname;
  680.                                 $return_val = true;
  681.                         }
  682.  
  683.                         return $return_val;
  684.                 }
  685.  
  686.                 /**********************************************************************
  687.                 *  Format a mySQL string correctly for safe mySQL insert
  688.                 *  (no mater if magic quotes are on or not)
  689.                 */
  690.  
  691.                 function escape($str)
  692.                 {
  693.                         // If there is no existing database connection then try to connect
  694.                         if ( ! isset($this->dbh) || ! $this->dbh )
  695.                         {
  696.                                 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  697.                                 $this->select($this->dbname);
  698.                         }
  699.  
  700.                         return mysql_real_escape_string(stripslashes($str));
  701.                 }
  702.  
  703.                 /**********************************************************************
  704.                 *  Return mySQL specific system date syntax
  705.                 *  i.e. Oracle: SYSDATE Mysql: NOW()
  706.                 */
  707.  
  708.                 function sysdate()
  709.                 {
  710.                         return 'NOW()';
  711.                 }
  712.  
  713.                 /**********************************************************************
  714.                 *  Perform mySQL query and try to detirmin result value
  715.                 */
  716.  
  717.                 function query($query)
  718.                 {
  719.  
  720.                         // Initialise return
  721.                         $return_val = 0;
  722.  
  723.                         // Flush cached values..
  724.                         $this->flush();
  725.  
  726.                         // For reg expressions
  727.                         $query = trim($query);
  728.  
  729.                         // Log how the function was called
  730.                         $this->func_call = "\$db->query(\"$query\")";
  731.  
  732.                         // Keep track of the last query for debug..
  733.                         $this->last_query = $query;
  734.  
  735.                         // Count how many queries there have been
  736.                         $this->num_queries++;
  737.  
  738.                         // Use core file cache function
  739.                         if ( $cache = $this->get_cache($query) )
  740.                         {
  741.                                 return $cache;
  742.                         }
  743.  
  744.                         // If there is no existing database connection then try to connect
  745.                         if ( ! isset($this->dbh) || ! $this->dbh )
  746.                         {
  747.                                 $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  748.                                 $this->select($this->dbname);
  749.                         }
  750.  
  751.                         // Perform the query via std mysql_query function..
  752.                         $this->result = @mysql_query($query,$this->dbh);
  753.  
  754.                         // If there is an error then take note of it..
  755.                         if ( $str = @mysql_error($this->dbh) )
  756.                         {
  757.                                 $is_insert = true;
  758.                                 $this->register_error($str);
  759.                                 $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  760.                                 return false;
  761.                         }
  762.  
  763.                         // Query was an insert, delete, update, replace
  764.                         $is_insert = false;
  765.                         if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
  766.                         {
  767.                                 $this->rows_affected = @mysql_affected_rows();
  768.  
  769.                                 // Take note of the insert_id
  770.                                 if ( preg_match("/^(insert|replace)\s+/i",$query) )
  771.                                 {
  772.                                         $this->insert_id = @mysql_insert_id($this->dbh);
  773.                                 }
  774.  
  775.                                 // Return number fo rows affected
  776.                                 $return_val = $this->rows_affected;
  777.                         }
  778.                         // Query was a select
  779.                         else
  780.                         {
  781.  
  782.                                 // Take note of column info
  783.                                 $i=0;
  784.                                 while ($i < @mysql_num_fields($this->result))
  785.                                 {
  786.                                         $this->col_info[$i] = @mysql_fetch_field($this->result);
  787.                                         $i++;
  788.                                 }
  789.  
  790.                                 // Store Query Results
  791.                                 $num_rows=0;
  792.                                 while ( $row = @mysql_fetch_object($this->result) )
  793.                                 {
  794.                                         // Store relults as an objects within main array
  795.                                         $this->last_result[$num_rows] = $row;
  796.                                         $num_rows++;
  797.                                 }
  798.  
  799.                                 @mysql_free_result($this->result);
  800.  
  801.                                 // Log number of rows the query returned
  802.                                 $this->num_rows = $num_rows;
  803.  
  804.                                 // Return number of rows selected
  805.                                 $return_val = $this->num_rows;
  806.                         }
  807.  
  808.                         // disk caching of queries
  809.                         $this->store_cache($query,$is_insert);
  810.  
  811.                         // If debug ALL queries
  812.                         $this->trace || $this->debug_all ? $this->debug() : null ;
  813.  
  814.                         return $return_val;
  815.  
  816.                 }
  817.  
  818.         }
  819.         $db = new ezSQL_mysql('root','something','somthign','localhost');
  820. ?>
Add Comment
Please, Sign In to add comment