Sponsored Links

Go Back   WebMasters.org Forums > Website Design & Development > PHP Forum

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-06-2008, 07:58 PM
Scott Scott is offline
Junior Member
 
Join Date: Jan 2008
Posts: 2
Default MySQL Library

Here is a MySQL library I wrote a while back. Enjoy.

PHP Code:
<?php
error_reporting
(E_ALL);
/*----------------------------------------------------------------*\
     Rival Webs   (http://www.rivalwebs.net               Version 0.0.9
  ----------------------------------------------------------------
     MySQL Database Driver (Rev: 5-20-2007)
     Copyright (c) 2004-2005 Scott Bedard.
\*----------------------------------------------------------------*/

/**
* MySQL Database Driver
* @author Scott Bedard
* @copyright Copyright &copy; 2004-2006 Scott Bedard.
* @version Revision: 5-20-2007
*/

// microtime
function start_clock() {
    global 
$query_time$query_count$time_start;
    
$time_start microtime(true);
    
$query_count++;
}

function 
stop_clock() {
    global 
$query_time$query_count$time_start;
    
$time_end microtime(true);
    
$time $time_end $time_start;
    
$query_time round($query_time $time4);
}

// sql_connect()
function sql_connect($servernull$databasenull$usernamenull$passwordnull) {
    if (
is_null($server) || is_null($database) || is_null($username) || is_null($password)) {
        return 
false;
    }
    
start_clock();
    
$connect mysql_connect($server$username$password);
    
stop_clock();
    
start_clock();
    
$db mysql_select_db($database);
    
stop_clock();
    if (!
$connect) {
        return 
false;
    } else {
        return 
true;
    }

}

// sql_return_connect()
function sql_connect_return($severnull$database null$usernamenull$password=null) {
    if (
is_null($server) || is_null($database) || is_null($username) || is_null($password)) {
        return 
false;
    }
    
$connect_return "mysql_connect('".$server."', '".$username."', '".$password."'):";
    
$db_return "mysql_select_db('".$database."');";
    return 
$connect_return." ".$db_return;
}

// sql_select()
function sql_select($tablenull$fieldnull$conditionnull) {
    if (
is_null($table) || is_null($field) || is_null($condition)) {
        return 
false;
      }
      
$i 0;
      
$condition_string '';
      
$last count($condition) - 1;
       foreach (
$condition as $a => $b) {
         
$condition_string .= "`".$a."` = '".$b."'";
         if (
$i $last)
        
$condition_string .= "AND ";
         
$i++;
      }
      if (
$field == '*') {
        
$field_string '*';
    } else {
        
$field_string "`".$field."`";
    }
    
$query "SELECT ".$field_string." from `".$table."` WHERE ".$condition_string;
    
start_clock();
    
$sql mysql_query($query);
    
stop_clock();
    if (!
$sql) {
        return 
false;
    } else {
        return 
mysql_fetch_array($sql);
    }
}

// sql_select_all()
function sql_select_all($tablenull$fieldnull$sortnull){ 
      if(
is_null($table)){ 
          return 
false
      } 
      if(
is_null($sort) || is_null($field)){ 
        
$query "SELECT * from `".$table."`"
      } else { 
        
$query "SELECT * from `".$table."` ORDER BY `".$field."` ".$sort
      } 
      
start_clock();
      
$sql mysql_query($query); 
      
stop_clock();
    
      while(
$row mysql_fetch_array($sql)){ 
        
$return[] = $row
      } 
           
      if(empty(
$return)) 
        return 
false
      else 
        return 
$return



// sql_select_array()
function sql_select_array($table null$field null$condition null,
    
$order null$key null)
{
    if (
is_null($table)) {
        return 
false;
      }
    
$query 'SELECT ';
    if (
is_null($field)) {
        
$query .= '* ';
    } else if (
is_string($field)) {
        
$query .= $field.' ';
    } else if (
is_array($field)) {
        
$query .= implode(', '$field);
    }
    
$query .= 'FROM '.$table;
    
    if (
is_string($condition)) {
        
$query .= ' WHERE '.$condition;
    } else {
        
$query .= ' WHERE ';
        
$conds = array();
        foreach (
$condition as $col => $val) {
            if (
is_array($val)) {
                
$conds[] = $col.' IN ('.implode(', 'array_map('_quote'$val)).
                    
')';
            } else {
                
$conds[] = $col.' = \''.$val.'\'';
            }
            
$query .= implode(' AND '$conds);
        }
    }
    if (!
is_null($order)) {
        if (
is_string($order)) {
            
$matches = array();
            
$mcnt preg_match_all('/(\w+)([+-])/'$order$matches,
                
PREG_SET_ORDER);
            
$order = array();
            foreach (
$matches as $m) {
                
$order[$m[1]] = ($m[2] == '+') ? 'ASC' 'DESC';
            }
        }
        
$o = array();
        foreach (
$order as $col => $dir) {
            switch (
strtolower($dir{0})) {
                case 
'a':
                case 
'+':
                    
$o[] = $col.' ASC';
                    break;
                case 
'd':
                case 
'-':
                    
$o[] = $col.' DESC';
                    break;
            }
        }
        
$query .= ' ORDER BY '.implode(', '$o);
    }
    
start_clock();
    
$reply mysql_query($query);
    
stop_clock();
    if (!
$reply) {
        return 
false;
    }
    if (
mysql_num_rows($reply) == 0) {
        return array();
    }
    
$ret = array();
    while (
$row mysql_fetch_assoc($reply)) {
        if (
$key) {
            
$ret[$row[$key]] = $row;
        } else {
            
$ret[] = $row;
        }
    }
    return 
$ret;
}

// sql_insert()
function sql_insert($table null$values null) {
    if (
is_null($table) || is_null($values)) {
        return 
false;
    }
    
$columns_specified false;
    foreach (
$values as $k => $v) {
        if (!
$columns_specified && is_string($k))
            
$columns_specified true;
        if (
is_null($v))
            
$values[$k] = 'NULL';
        else
            
$values[$k] = '\''.mysql_real_escape_string($v).'\'';
    }
    
$value_str implode(', '$values);
    
$query 'INSERT INTO '.$table.' ';
    if (
$columns_specified) {
        
$query .= '('.implode(', 'array_keys($values)).') ';
    }
    
$query .= 'VALUES ('.$value_str.')';
    
start_clock();
    
$result mysql_query($query);
    
stop_clock();
    if (!
$result) {
        return 
false;
    } else {
        return ((
$id mysql_insert_id()) === 0) ? true $id;
    }
}

// sql_update()
function sql_update($tablenull$fieldsnull$conditionnull$limitnull) {
    if (
is_null($table) || is_null($fields) || is_null($condition)) {
        return 
false;
    }
    if (
is_null($limit)) {
        
$limit '1';
    }
    
$i 0;
    
$field_string '';
    
$last count($fields) - 1;
    foreach (
$fields as $a => $b) {
        
$field_string .= "`".$a."`='".$b."'";
        if (
$i $last) {
            
$field_string .= ', ';
        }
        
$i++;
    }
    
$i 0;
    
$condition_string '';
    
$last count($condition) - 1;
    foreach (
$condition as $a => $b) {
        
$condition_string .= "`".$a."`='".$b."'";
        if (
$i $last) {
            
$condition_string .= 'AND ';
        }
        
$i++;
    }
    
$query "UPDATE `".$table."` SET ".$field_string." WHERE ".$condition_string." LIMIT 1";
    
start_clock();
    
$sql mysql_query($query);
    
stop_clock();
    if (!
$sql) {
        return 
false;
    } else {
        return 
true;
    }
}

// sql_delete()
function sql_delete($table null$condition null) {
    if (
is_null($table) || is_null($condition)) {
        return 
false;
    }
    
$i 0;
    
$condition_string '';
    
$last count($condition) - 1;
    foreach (
$condition as $a => $b) {
        
$condition_string .= "`".$a."`='".$b."'";
    if (
$i $last)
        
$condition_string .= 'AND ';
        
$i++;
    }
    
$query 'DELETE FROM `'.$table.'` WHERE '.$condition_string;
    
start_clock();
    
$sql mysql_query($query);
    
stop_clock();
    if (!
$sql) {
        return 
false;
    } else {
        return 
true;
    }
}

// sql_count()
function sql_count($table null) {
    if (
is_null($table)) {
        return 
0;
    } else {
        
start_clock();
        
$query mysql_query("SELECT * FROM ".$table);
        
$rows mysql_num_rows($query);
        
stop_clock();
        return 
$rows;
    }
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-23-2008, 12:49 AM
Amirmullick3 Amirmullick3 is offline
Junior Member
 
Join Date: Jan 2008
Posts: 5
Default

Thats really helpful! Just what I needd!! Thanks a lot!
__________________
www.stuffworth.com

Get all your psp ISO's, ps2 games, xbox360 games, wii games, pc games, and various applications for free at StuffWorth! Register today!

www.ayrshire-webdesign.com/site

Get the best hosting plans on the net here! Easy to navigate website, and affordable hosting for all domains!

www.ukfilesharing.com

Upload and share all your files with ukfs! Upload files of 500mb! Uploads are never deleted!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-26-2008, 08:16 PM
TheGamerX TheGamerX is offline
Senior Member
 
Join Date: Aug 2008
Posts: 107
Default

Please explain what these scripts do....
__________________
My favorite kind of Online games are
MMORPG | MMO | Free MMORPG |Free MMO | Games like MapleStory and Warcraft are amazing . MMORPGs are fun because they're social. You interact with more people.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:01 PM.


Sponsored Links

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0
vB Ad Management by =RedTyger=