mysql操作函数库

<?php
// ******************************* Table of Contents *****************************************

//    -  _mysql();
//    -  mysql_get ();
//    -  mysql_get_row ();
//    -  mysql_get_value ();
//    -  mysql_add ();
//    -  mysql_edit ();
//    -  mysql_delete ();
//    -  mysql_value_exists ();
//    -  build_mysql_search_query ();
//    -  get_tables();
//    -  get_fields();//http://www.viatop.cn/blog

// *************************** Main functions - Prototypes ***********************************

// function _mysql( $query, [$returns_array = 2,] [$type = "BOTH"] );
// function mysql_get ( $select, $from, [$where = "",] [$order = "",] [$start = 0,] [$limit = 0] );
// function mysql_get_row ( $select, $from, [$where = ""] );
// function mysql_get_value ( $select, $from, [$where = ""] );
// function mysql_add ( $table, $columns, $data, [$delimiter = ", "] );
// function mysql_edit ( $table, $columns, $data, $where, [$priority = "LOW"], [$delimiter = ", "], [$debug = ""] );
// function mysql_delete ( $table, $where );
// function mysql_value_exists ( $table, $string, $field, [$returned_field = FALSE] );
// function build_mysql_search_query ( $columns, $data );
// function get_tables( [$search_string = "",] [$order = "ASC"] );
// function get_fields( $lookup_table, [$search_string = "",] [$order = "NONE"] );
//http://www.viatop.cn/blog

// *************************** MySQL debugging/tracking vars *********************************
// *
// * When $SQL_DEBUG_MODE is enabled, MySQL commands will send HTML comment of the exact
// *      query line that was constructed.
// * A footer file would have access to $MSQL_*_CALLS and would be a good place to display
// *      them for debugging. Especially for performance issues.
// * $SQL_DEBUG_MODE may be enabled at any point in any script. If it is enabled in this .h
// *      file, it may cause interference with headers, such as cookies.
// ***

$SQL_DEBUG_MODE = false;
$_MYSQL_CALLS = 0;
$MYSQL_GET_CALLS = 0;
$MYSQL_ADD_CALLS = 0;
$MYSQL_EDIT_CALLS = 0;
$MYSQL_DELETE_CALLS = 0;
$MYSQL_TOTAL_CALLS = 0;

function _mysql( $query, $returns_array = 2, $type = "BOTH")
{
 global $db;
 global $MYSQL_GET_CALLS, $MYSQL_ADD_CALLS,
     $MYSQL_EDIT_CALLS, $MYSQL_DELETE_CALLS,
     $MYSQL_TOTAL_CALLS, $_MYSQL_CALLS;
 global $SQL_DEBUG_MODE;

 $_MYSQL_CALLS++;
 $MYSQL_TOTAL_CALLS++;

// this line allows the user to pass in $type as the 2nd param, and assume "2" for $returns_array
 if ($returns_array == "ASSOC" || $returns_array == "NUM" || $returns_array == "BOTH") {
  $type = $returns_array;
  $returns_array = 2;
 }
 //echo $query;
 if ( !($result = mysql_select_db(DB_NAME, $db)) )
 {
  echo "nn<b>ERROR mysql_get(): Failed to access the database (mysql_select_db).</b><br>n";
  exit;
  return 0;
 }

 if ($SQL_DEBUG_MODE)
  comment("nmysql_get () ### " . $query);

 if ( !($result = mysql_query($query, $db)) )
 {
  echo "nn<b>ERROR _mysql(): on query: "" . strtoupper(substr($query, 0, 6)) . "" (at mysql_query). Query failed.</b><br>n";
  echo "nn<!-- ### Query Attempted: ($query) -->nn";
  return 0;
 }

 switch ( strtoupper(substr($query, 0, 6)) )
 {
  case "UPDATE":
   return 1;
   break;
  case "DELETE":
   return 1;
   break;
  case "INSERT":
   return mysql_insert_id();
   break;
  case "SELECT":

   if ($type == "ASSOC")
    for ( $a = 0; $row = @mysql_fetch_array($result, MYSQL_ASSOC); $a++ )
     $allitems[$a] = $row;
   else if ($type == "NUM")
    for ( $a = 0; $row = @mysql_fetch_array($result, MYSQL_NUM); $a++ )
     $allitems[$a] = $row;
   else
    for ( $a = 0; $row = @mysql_fetch_array($result); $a++ )
     $allitems[$a] = $row;

// can this be made more dynamic ... ??? ^ v

   if ($returns_array == "val")
    return $allitems[0][0];
   else if ($returns_array == "row")
    return $allitems[0];
   else
    return $allitems;

   break;

  default:
   break;
 }

// This converts a two-dimensional, single-item array to a single-dimensional array
// if ( count($allitems) == 1 )
//  $allitems = $allitems[0];

}

//$select 表里面的字段,*
//$from 表名
//$where 条件 不要带where
//$order 排序 不要带order by
//$start  $limit 缺省limit
//$result_type assoc数组索引里面没有数字项

/********** Gets all specified data from an SQL table, returns array ********/
function mysql_get ( $select, $from, $where = "", $order = "", $start = 0, $limit = 0,
      $result_type = "" )
{
 global $db;
 global $MYSQL_GET_CALLS, $MYSQL_ADD_CALLS,
     $MYSQL_EDIT_CALLS, $MYSQL_DELETE_CALLS,
     $MYSQL_TOTAL_CALLS;
 global $SQL_DEBUG_MODE;

 $MYSQL_GET_CALLS++;
 $MYSQL_TOTAL_CALLS++;

 if ( !($result = mysql_select_db(DB_NAME, $db)) )
 {
  echo "nn<b>ERROR mysql_get(): Failed to access the database (mysql_select_db).</b><br>n";
  return 0;
 }

 switch ( strtoupper($result_type) )
 {
  case "BOTH":
  case "NUM":
  case "ASSOC":
   $result_type = "MYSQL_" . $result_type; break;
  default:
   $result_type = "MYSQL_BOTH";
 }

 $build_query = "SELECT " . $select . " FROM " . $from;

 if ($where)
  $build_query .= " WHERE " . $where;

 if ($order)
  $build_query .= " ORDER BY " . $order;

 if ($limit)
  $build_query .= " LIMIT " . $start . ", " . $limit;

 if ($SQL_DEBUG_MODE)
  comment("nmysql_get () ### " . $build_query);
  
// echo $build_query;
 $result = mysql_query($build_query, $db);
 $allitems = array ();
 for ( $a = 0; $row = @mysql_fetch_array($result); $a++ )
  $allitems[$a] = $row;

// This converts a two-dimensional, single-item array to a single-dimensional array
// if ( count($allitems) == 1 )
//  $allitems = $allitems[0];

 return $allitems;
}

/****** Gets one specified piece of data fom sql table, returns value ******/
function mysql_get_row ( $select, $from, $where = "" )
{
 $data = mysql_get($select, $from, $where, "", 0, 1);
 return $data[0];
}

/****** Gets one specified piece of data fom sql table, returns value ******/
function mysql_get_value ( $select, $from, $where = "" )
{
 $data = mysql_get($select, $from, $where, "", 0, 1);
 if ($data != null) {
  return $data[0][0];
 } else {
  return null;
 }
}

/********** Adds a line to mySQL database table, returns true or false ********/
function mysql_add ( $table, $columns, $data, $delimiter = ", " )
{
 global $db;
 global $MYSQL_GET_CALLS, $MYSQL_ADD_CALLS,
     $MYSQL_EDIT_CALLS, $MYSQL_DELETE_CALLS,
     $MYSQL_TOTAL_CALLS;
 global $SQL_DEBUG_MODE;
 $val_query="";

 $MYSQL_ADD_CALLS++;
 $MYSQL_TOTAL_CALLS++;

 if ( !($result = mysql_select_db(DB_NAME, $db)) )
 {
  echo "nn<b>ERROR mysql_add(): Failed select a database database (" . DB_NAME . ").</b><br>n";
  return 0;
 }

 $vals_array = explode($delimiter, $data);

 for ($a = 0; $a < count($vals_array); $a++)
 {
  $cur_val = trim(addslashes($vals_array[$a]));

  $val_query .= "'" . $cur_val . "'";

  if ($a < (count($vals_array) - 1))
   $val_query .= ", ";
 }

 $full_query = "INSERT INTO " . $table .
      " (" . $columns . ") " .
      "VALUES (" . $val_query . ")";

 //echo $full_query;
 
 if ($SQL_DEBUG_MODE)
  comment ("mysql_add () ### " . $full_query);

 if ( !($result = mysql_query($full_query, $db) or die(mysql_error())) )
 {
  echo "nn<b>ERROR mysql_add(): Failed to add to database (at mysql_query). Query failed. (Delim=$delimiter)</b><br>n";
  return 0;
 }

 return mysql_insert_id();
}

/********** Edit a line in mySQL database table, returns true or false ********/
function mysql_edit ( $table, $columns, $data, $where, $delimiter = ", ")
{
 global $db;
 global $MYSQL_GET_CALLS, $MYSQL_ADD_CALLS,
     $MYSQL_EDIT_CALLS, $MYSQL_DELETE_CALLS,
     $MYSQL_TOTAL_CALLS;
 global $SQL_DEBUG_MODE;
 $val_query="";
 $priority="";

 $MYSQL_EDIT_CALLS++;
 $MYSQL_TOTAL_CALLS++;

 if (!$delimiter) $delimiter = ", ";

 $priority = strtoupper($priority);

 if ( !($result = mysql_select_db(DB_NAME, $db)) )
 {
  echo "nn<b>ERROR mysql_edit(): Failed to open database (mysql_select_db).";
  if (!DB_NAME)
   echo " DB_NAME undefined.";
  echo "</b><br>n";
  return 0;
 }

 $columns_array = explode(", ", $columns);
 $vals_array = explode($delimiter, $data);

 $col_count = count($columns_array);
 $val_count = count($vals_array);

 if ( $col_count != $val_count )
 {
  echo "ERROR mysql_edit(): Argument count mismatch. Data may contain delimiter (" . $delimiter . ").";
  return 0;
 }

 for ($a = 0; $a < $val_count; $a++)
 {
  $cur_val = trim(addslashes($vals_array[$a]));

  $val_query .= $columns_array[$a] . "='" . $cur_val . "'";

  if ($a < ($val_count - 1))
   $val_query .= ", ";
 }

 $full_query = "UPDATE " . // . $priority . " PRIORITY " . $table .
      $table . " SET " . $val_query . " WHERE " . $where;
     
 //echo $full_query;

 if ($SQL_DEBUG_MODE)
  comment ( ("mysql_edit() ### " . $full_query) );

 if ( !($result = @mysql_query($full_query, $db) or die(mysql_error())) )
  echo "nn<b>ERROR mysql_edit(): Database query failed. (mysql_query).</b><br>n";

 return 1;
}

/********** Deletes a line from mySQL database table, returns true or false ********/
function mysql_delete ( $table, $where )
{
 global $db;
 global $MYSQL_GET_CALLS, $MYSQL_ADD_CALLS,
     $MYSQL_EDIT_CALLS, $MYSQL_DELETE_CALLS,
     $MYSQL_TOTAL_CALLS;
 global $SQL_DEBUG_MODE;

 $MYSQL_DELETE_CALLS++;
 $MYSQL_TOTAL_CALLS++;

 if ( !($result = mysql_select_db(DB_NAME, $db)) )
 {
  echo "nn<b>ERROR mysql_delete(): Failed to open the database.</b><br>n";
  return 0;
 }

 $full_query = "DELETE FROM " . $table .
      " WHERE " . $where;
 //echo $full_query;
 if ($SQL_DEBUG_MODE)
  comment ("mysql_delete() ### " . $full_query);

 if ( !($result = @mysql_query($full_query, $db)) )
 {
  echo "nn<b>ERROR mysql_delete(): Failed to delete from database (mysql_query).</b><br>n";
  return 0;
 }

 return 1;
}

/********** Checks that $string exists in SQL $table, returns false, true, or values ********/
function mysql_value_exists($table, $string, $field, $returned_field = FALSE)
{
 $all_rows = array("");
 $i=0;
 $result = mysql_get($field, $table, "$field='$string'");
 return $result;
}
?>

1条评论

viatop

是用方法可以见下面的最新更新例子
//最新更新
$arrMessage = mysql_get('*', $tbl_message, '', 'timestamp desc', 0, 8);
for ($i=0; $i

';
}

回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注

😉😐😡😈🙂😯🙁🙄😛😳😮:mrgreen:😆💡😀👿😥😎😕