WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Function for field query
Categories : PHP, Databases, MySQL Click here to Update Your Picture
lalith nayak
Date : Feb 28th 2006
Grade : 1 of 5 (graded 4 times)
Viewed : 2100
File : No file for this code example.
Images : No Images for this code example.
Search : More code by lalith nayak
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This is a small function I wrote to handle queries on a table.
It can query a table, order and sort, and supports inner joins.

This function also returns the result as a single row or all rows.

Enjoy :

<?php
 
/**
   * Selects Fields from a database/table. 
   * Also supports INNER JOINS, GROUP BY, ORDER BY.
   *
   *
   * @param string $db_name        //Name of Database
   * @param string $table          //Name of Table
   * @param array $fields          //Field Names we want to select
   * @param array $inner            //Fields to be INNER JOINED
   * @param array $where            //WHERE fields.
   * @param string $group_by        //Field to group by.
   * @param string $order_by        //Field to order by
   * @param string $asc            //Asc or Desc order
   * @param string $return_array    //Defines $rst as an array or single row
   * @return $rst                  //Returns the result
   *
   * @example : (1) getFields('mytool','master',
   *                          'array('master.master_id','master.fname_id'));
   *            (2) getFields('mytool','master',
   *                          'array('master.master_id','master.fname_id',
   *                          'fname.fname'), array(0=>array('mytool',
   *                          'fname','master','fname_id',)),
   *                          'master.default_value <> "", false);
   *            (3) getFields('mytool','master','master.master_id',NULL,
   *                          'master.fname_id = "3",NULL,NULL,NULL,false);
   */
 
function getFields($db_name=NULL,$table=NULL,$fields=NULL,$inner=NULL,
                     
$where=NULL,$group_by=NULL,$order_by=NULL,$asc=NULL,
                     
$return_array=true) {
   
$C_NAME = __CLASS__."::".__FUNCTION__;
   
$sql = "SELECT ";
   
is_array($fields) ? $sql .= implode(',',$fields) : $sql .= $fields;
   
$sql .= " FROM ".$db_name.".".$table;
   if(!empty(
$inner)) {
     
$total_inners = count($inner);
     for(
$i=0; $i<$total_inners; $i++)
       
$sql .= " INNER JOIN ".$inner[$i][0].".".$inner[$i][1]."
                 ON "
.$inner[$i][1].".".$inner[$i][2]." =
                     "
.$inner[$i][3].".".$inner[$i][2];
   }
   if(!empty(
$where)) {
     
is_array($where) ? $sql .= " WHERE ".implode(' AND ',$where)
                       : die(
"Fatal Error : Variable must be an array. 
                         Variable type is : "
.gettype($where));
   }
   !empty(
$group_by) ? $sql .= " GROUP BY ".$group_by : '';
   !empty(
$order_by) ? $sql .= " ORDER BY ".$order_by : '';
   !empty(
$asc)      ? $sql .= " ".$asc : '';
   
$sql .= " # Query resides in ".__FILE__."->".$C_NAME."
               on line "
.__LINE__;
   if(
$return_array === true) {
     
$rst = mysql_query($sql)
             or die(
"An Error Has Occurred!
                 \n MySQL Error Reports : "
.mysql_error()."
                 \n The Error Occured in "
.__FILE__." -> ".$C_NAME);
     
$i=0;
     while(
$row = mysql_fetch_assoc($rst)) {
       
$rowRst[] = $row[$fields[$i]];
       
$i++;
     }
   } else {
     
$rst = mysql_query($sql)
             or die(
"An Error Has Occurred!
                 \n MySQL Error Reports : "
.mysql_error()."
                 \n The Error Occured in "
.__FILE__." -> ".$C_NAME);
     
$rowRst = mysql_fetch_assoc($rst);
   }
   return
$rowRst;
  }
?>

-themightyindian



This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
Alternating background color for HTML table rows
Categories : PHP, Databases, MySQL, HTML and PHP
color codes for positive and negative numbers
Categories : PHP, MySQL, Databases, HTML
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Linked comboboxes with php-mysql & javascript
Categories : PHP, Java Script, Databases, MySQL
mysql_escape_string
Categories : PHP, MySQL, Databases, Strings
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables