Location: PHPKode > projects > Bradabra > bradabra/include/sql.php
<?php
//****************************************************************************
//
// Copyright (C) 2001 Eric SEIGNE <hide@address.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//****************************************************************************
//
// For more informations, get to the project's main source file.
//
// Description  SQL functions handlers.
//              Now, handle PostgreSQL and MySQL.
//
// Remark       This source is _highly_ inspired from the *sql_erics.php
//              files originally written by Eric Seigne <hide@address.com>
//              <www.rycks.com> delivered with the AbulEdu project cd-rom
//              <www.abuledu.org>, and others...
//
//****************************************************************************

if(!defined("__sql_php__")):
define("__sql_php__", "1");


//error_reporting(0);
define("MSG_UNKNOWNENGINE", "Unknown SQL engine !");

//----------------------------------------------------------------------------
// sql_connect
//----------------------------------------------------------------------------
function sql_connect ($basename, $user, $pass, $host, $port)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    if(empty($basename))
      return FALSE;

    $param = "dbname=".$basename;

    if(!empty($user))
    {
      $param .= " user=".$user;

      if(!empty($pass))
        $param .= " password=".$pass;
    }

    if(!empty($host))
    {
      $param .= " host=".$host;

      if(!empty($port))
        $param .= " port=".$port;
    }

    $conn = pg_connect($param);
    $GLOBALS["db_conn_pgsql"] = $conn;

    return $conn;
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    if(empty($basename))
      return FALSE;

    if(!empty($host))
    {
      $param = "$host";
      if(!empty($port))
  	    $param .= ":$port";
    }

    $conn = mysql_connect($param, $user, $pass);
    if(!$conn)
      return FALSE;

    mysql_select_db($basename);

    return $conn;
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_pconnect
//----------------------------------------------------------------------------
function sql_pconnect ($basename, $user, $pass, $host, $port)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    if(empty($basename))
      return FALSE;

    $param = "dbname=$basename";
    if(!empty($user))
    {
      $param .= " user=$user";
      if(!empty($pass))
        $param .= " password=$pass";
    }
    if(!empty($host))
    {
      $param .= " host=$host";
      if(empty($port))
        $param .= " port=$port";
    }

    return pg_pconnect($param);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    if(empty($basename))
      return FALSE;

    if(!empty($host))
    {
      $param = "$host";
      if(!empty($port))
        $param .= ":$port";
    }

    $conn = mysql_pconnect($param, $user, $pass);
    mysql_select_db($basename);

    return $conn;
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_close
//----------------------------------------------------------------------------
function sql_close ($conn=NULL)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    if(!$conn)
      return pg_close($GLOBALS["db_conn_pgsql"]);
    else
      return pg_close($conn);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    if(!$conn)
    {
      //return mysql_close($GLOBALS["db_conn_mysql"]);
      return TRUE;
    }
    else
      return mysql_close($conn);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_error
//----------------------------------------------------------------------------
function sql_error ()
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    if(!$GLOBALS["db_conn_pgsql"])
      return "Invalid database connection index (" . $GLOBALS["db_conn_pgsql"] . ").";

    return pg_errormessage($GLOBALS["db_conn_pgsql"]);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_error();
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_exec
//----------------------------------------------------------------------------
function sql_exec ($request, $conn=NULL)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    $GLOBALS["postgresql_row"] = 0;

    if(!$conn)
      $res = pg_exec($GLOBALS["db_conn_pgsql"], $request);
    else
      $res = pg_exec($conn, $request);

    if(!$res)
      return $res;

    $GLOBALS["postgresql_numrows"] = pg_numrows($res);
    return $res;
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    if(!$conn)
      return mysql_query($request);
    else
      return mysql_query($request, $conn);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_fatalerror
//----------------------------------------------------------------------------
function sql_fatalerror ($msg)
{
  print("\n");
  print("<h2>Error</h2>\n");
  print("<!--begin_error(sql_error)-->\n");
  print($msg."\n");
  print("<!--end_error-->\n");

  exit;
}

//----------------------------------------------------------------------------
// sql_fetcharray
//----------------------------------------------------------------------------
function sql_fetcharray ($result_id)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    if($GLOBALS["postgresql_numrows"] > $GLOBALS["postgresql_row"])
    {
      $r =  pg_fetch_array($result_id, $GLOBALS["postgresql_row"]++);
      if(!$r) {
        echo "Unable to fetch row\n";
        return '';
      }
    }
    else
      $r = '';

    return $r;
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_fetch_array($result_id);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_fetchrow
//----------------------------------------------------------------------------
function sql_fetchrow ($result_id)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    return pg_fetch_row($result_id);//, 0);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_fetch_row($result_id);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_fieldname
//----------------------------------------------------------------------------
function sql_fieldname ($result_id, $i)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    return pg_fieldname($result_id, $i);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_field_name($resultat, $i);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_freeresult
//----------------------------------------------------------------------------
function sql_freeresult ($result_id)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    return pg_freeresult($result_id);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_free_result($result_id);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_numfields
//----------------------------------------------------------------------------
function sql_numfields ($result_id)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    return pg_numfields($result_id);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_num_fields($result_id);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_numrows
//----------------------------------------------------------------------------
function sql_numrows ($result_id)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    return pg_numrows($result_id);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_num_rows($result_id);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}

//----------------------------------------------------------------------------
// sql_result
//----------------------------------------------------------------------------
function sql_result ($result_id, $i, $j)
{
  // PostgreSQL
  if(DB_ENGINE == "postgresql")
  {
    return pg_result($result_id, $i, $j);
  }

  // MySQL
  else if(DB_ENGINE == "mysql")
  {
    return mysql_result($result_id, $i, $j);
  }

  // Unknown SQL engine
  else
  {
    sql_fatalerror(MSG_UNKNOWNENGINE);
  }
}


endif;
?>
Return current item: Bradabra