Results 1 to 5 of 5
  1. #1

    [PHP]Snippet - MYSQL

    PHP Code:
    function updaterow($row = array()) {
        
        
    $querystring "";
        foreach(
    $row as $a=>$b) {
            
    $querystring .= "$a='$b',";
        }
        
    $querystring rtrim($querystring",");
        
        
    $query mysql_query("UPDATE table  SET $querystring ");
        

    simple update query..though it could be any..just insert array and it's updates.
    Currently working on a RPG.

    http://crusher.megabyet.net -RPG Develop BLOG

  2. #2
    Coders muppetalert's Avatar
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    1,006

    Re: [PHP]Snippet - MYSQL

    What you really want is a complete MySQL Querying framework.... You can then change the call from mysql_query to something else.... Good DAL if you ever need it.

    PHP Code:
    <?php

    /***********************************************
     * MAGE UK LTD GLOBAL WEBSITE FRAMEWORK
     * This file and any associated files within
     * /MageUK_Framework/* may not be distributed,
     * used or reproduced (in part or whole) by any
     * third party without the express written
     * permission and/or license of Mage UK Ltd
     * Licensing Department.
     *
     *  Mage UK Ltd.
     **********************************************/
     
    /* SimpleDatabase.php
     * Title: Simple Database Abstraction Layer
     * Author: Rudi Visser
     * Revision: 28
     */

    class SimpleDatabase {
        
        
    /* @private
         * Just store a reference to the parent.
         */
        
    private $Parent;
        public function 
    __construct($_parent) {
            
    $this->Parent $_parent;
            
    $this->Debug false;
            
    $this->DebugQCount 0;
        }
        
        public 
    $Debug;
        private 
    $DebugQCount;
        
        
    /* @public
         * Safely insert data into an open database connection.
         */
        
    public function Safe_Insert($tableName$newData) {
            
    $this->DebugQCount++;
            foreach (
    $newData as $field => $value) {
                
    $fields[] = '`' $field '`';
                if(
    $value == "NOW()"$values[] = $value;
                else 
    $values[] = "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            
            
    $field_list join(', '$fields);
            
    $value_list join(', '$values);
            
            
    $query 'INSERT INTO `'.$tableName.'` ('.$field_list.') VALUES ('.$value_list.');';
            return 
    mysql_query($query);
        }
        
        
    /* @public
         * Safely update data in a table via an open database connection.
         */
        
    public function Safe_Update($tableName$newData$whereClause$singleUpdate false) {
            
    $this->DebugQCount++;
            
    $buildSet "SET";
            foreach (
    $newData as $field => $value) {
                if(
    $buildSet != "SET"$buildSet .= ",";
                
    $buildSet .= ' `' $field '` = ';
                if(
    $value == "NOW()"$buildSet .= $value;
                else 
    $buildSet .= "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            
            
    $query 'UPDATE `'.$tableName.'` '.$buildSet.' WHERE '.$whereClause.($singleUpdate?" LIMIT 1":"").';';
            return 
    mysql_query($query);
        }
        
        
    /* @public
         * Check if a specific row exists
         */
        
    public function Is_Row_Present($tableName$searchData = array()) {
            
    $this->DebugQCount++;
            
    $buildWhere "";
            foreach (
    $searchData as $field => $value) {
                if(
    $buildWhere != ""$buildWhere .= " AND";
                
    $buildWhere .= ' `' $field '` = ' "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            
    $resultCount mysql_num_rows(mysql_query('SELECT * FROM `'.$tableName.'` WHERE '.$buildWhere.' LIMIT 1;'));
            if(
    $resultCount == 1) return true;
            else return 
    false;
        }
        
        
    /* @public (Rev 28)
         * Check if a specific row exists
         */
        
    public function Row_Amount($tableName$searchData = array()) {
            
    $this->DebugQCount++;
            
    $buildWhere "";
            foreach (
    $searchData as $field => $value) {
                if(
    $buildWhere != ""$buildWhere .= " AND";
                
    $buildWhere .= ' `' $field '` = ' "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            if(
    $this->Debug) { echo '<!-- (Query '.$this->DebugQCount.') Row_Amount("'.$tableName.'", ...) : '$start microtime(); }
            
    $resultCount mysql_fetch_row(mysql_query('SELECT COUNT(*) FROM `'.$tableName.'`'.($buildWhere!=""?' WHERE '.$buildWhere:"").';'));
            if(
    $this->Debug) { $end microtime(); echo $end-$start" -->\n"; }
            return 
    $resultCount[0];
        }
        
        
    /* @public
         * Get a single row into an associative array
         */
        
    public function Get_Associative_Record($tableName$searchData = array()) {
            
    $this->DebugQCount++;
            
    $buildWhere "";
            foreach (
    $searchData as $field => $value) {
                if(
    $buildWhere != ""$buildWhere .= " AND";
                
    $buildWhere .= ' `' $field '` = ' "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            return 
    mysql_fetch_assoc(mysql_query('SELECT * FROM `'.$tableName.'` WHERE '.$buildWhere.' LIMIT 1;'));
        }
        
        
    /* @public
         * Get an array of associative result records from a search
         */
        
    public function Get_Associative_Records($tableName$searchData = array(), $limit "") {
            
    $this->DebugQCount++;
            
    $buildWhere "";
            foreach (
    $searchData as $field => $value) {
                if(
    $buildWhere != ""$buildWhere .= " AND";
                
    $buildWhere .= ' `' $field '` = ' "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            if(
    $this->Debug) { echo '<!-- (Query '.$this->DebugQCount.') Get_Associative_Records("'.$tableName.'", ...) : '$start microtime(); }
            
    $results mysql_query('SELECT * FROM `'.$tableName.'`'.($buildWhere!=""?' WHERE '.$buildWhere:"").($limit!=""?' LIMIT '.$limit:"").';');
            
    $returnMe = array();
            while(
    $row mysql_fetch_assoc($results)) {
                
    $returnMe[] = $row;
            }
            if(
    $this->Debug) { $end microtime(); echo $end-$start" -->\n"; }
            return 
    $returnMe;
        }
        
        
    /* @public
         * Get an array of associative result records from a search
         */
        
    public function Get_Associative_Records_Indexed($tableName$indexRow "id"$searchData = array(), $limit ""$sort=false$textRow="") {
            
    $this->DebugQCount++;
            
    $buildWhere "";
            foreach (
    $searchData as $field => $value) {
                if(
    $buildWhere != ""$buildWhere .= " AND";
                
    $buildWhere .= ' `' $field '` = ' "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            if(
    $this->Debug) { echo '<!-- (Query '.$this->DebugQCount.') Get_Associative_Records_Indexed("'.$tableName.'", ...) : '$start microtime(); }
            
    $results mysql_query('SELECT * FROM `'.$tableName.'`'.($buildWhere!=""?' WHERE '.$buildWhere:"").($limit!=""?' LIMIT '.$limit:"").';');
            if(
    $sort) { $tempArr = array(); $tempArr_Row = array(); }
            
    $returnMe = array();
            while(
    $row mysql_fetch_assoc($results)) {
                if(
    $sort) {
                    
    $tempArr[$row[$indexRow]] = $row[$textRow];
                    
    $tempArr_Row[$row[$indexRow]] = $row;
                }
                
    $returnMe[$row[$indexRow]] = $row;
            }
            if(
    $sort) {
                
    $returnMe = array();
                
    asort($tempArr);
                foreach(
    $tempArr As $Index => $Value) {
                    
    $returnMe[$Index] = $tempArr_Row[$Index];
                }
            }
            if(
    $this->Debug) { $end microtime(); echo $end-$start" -->\n"; }
            return 
    $returnMe;
        }
        
        public function 
    Delete_Row($tableName$searchData = array(), $limit 0) {
            
    $this->DebugQCount++;
            
    $buildWhere "";
            foreach (
    $searchData as $field => $value) {
                if(
    $buildWhere != ""$buildWhere .= " AND";
                
    $buildWhere .= ' `' $field '` = ' "'" mysql_real_escape_string(htmlentities($value)) . "'";
            }
            
    mysql_query('DELETE FROM `'.$tableName.'`'.($buildWhere!=""?' WHERE '.$buildWhere:"").($limit>0?' LIMIT '.$limit:"").';');
            return 
    true;
        }
        
        
    /* @public (Rev 27)
         * Connect to a database server.
         */
        
    public function Connect_To_Server($server$user$password$dbname) {
            @
    mysql_connect($server$user$password) or die('<span style="font-family:Tahoma;font-size:12px"><b>! MageUK Framework-&gt;'.get_class($this).' ~</b> Connect to '.$server.' failed.</span>');
            @
    mysql_select_db($dbname) or die('<span style="font-family:Tahoma;font-size:12px"><b>! MageUK Framework-&gt;'.get_class($this).' ~</b> Selecting database `'.$dbname.'` failed.</span>');
        }
        
        
    /** iModule Methods **/
        
    public function GetName() {
            return 
    "Simple Database Module (mysql)";
        }
        public function 
    GetDescription() {
            return 
    "Simple module for mysql, uses already connected mysql interface. Provides method to instantiate a connection.";
        }
        public function 
    GetAuthor() {
            return 
    "Rudi Visser";
        }
        public function 
    GetRevision() {
            return 
    28;
        }
        public function 
    PrintDetails() {
            echo 
    "<b>" $this->GetName() . "</b><br />" $this->GetDescription() . "<br />Author: " $this->GetAuthor() . "<br />Revision: ".$this->GetRevision();
        }
    }

    ?>
    whiteLight: ET, whiteLight: Quake Live (Formerly QLHook) and many other game hacks at http://whitelight.me.uk/

  3. #3

    Re: [PHP]Snippet - MYSQL

    Nice framework! Though if i am not wrong it also includes some mysql error_handling.
    Currently working on a RPG.

    http://crusher.megabyet.net -RPG Develop BLOG

  4. #4
    Coders muppetalert's Avatar
    Join Date
    Nov 2007
    Location
    Manchester, UK
    Posts
    1,006

    Re: [PHP]Snippet - MYSQL

    Nope it doesn't, but could be easily expanded to do so...

    I used the wrong words in my last post, I meant to say that you should work towards a true framework that emulates methods in a uniform way, so that you're able to substitute between mysql and other database systems easily.
    whiteLight: ET, whiteLight: Quake Live (Formerly QLHook) and many other game hacks at http://whitelight.me.uk/

  5. #5

    Re: [PHP]Snippet - MYSQL

    oh..Thanks for tip :P
    Currently working on a RPG.

    http://crusher.megabyet.net -RPG Develop BLOG

Similar Threads

  1. Replies: 14
    Last Post: May 23rd, 2013, 06:50
  2. Replies: 2
    Last Post: June 7th, 2010, 14:45
  3. Offering help on Mysql, php and css
    By michael1987 in forum Html, Php, Xml, Css, ...
    Replies: 1
    Last Post: January 27th, 2009, 19:40
  4. Log in at website (MySQL?)
    By Square in forum Html, Php, Xml, Css, ...
    Replies: 6
    Last Post: December 8th, 2008, 18:39
  5. Replies: 3
    Last Post: September 11th, 2007, 10:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •