[MySQL][PHP] Universal Select / Update

Status
Not open for further replies.

xenogfx

New Member
Oct 24, 2011
24
12
Want to select and update any one column and any one table? Well here is how you do it :)

PHP:
<?php
 
/**
* @Author Deformed aka XenoGFX
* @Copyright 2012
* @Description Simple server UPDATE and SELECT query functions..
*/
 
 
// Update Query
function mSU($table,$column,$value,$condition,$limit)
{
    mysql_query("UPDATE $table SET $column = '$value' WHERE $condition LIMIT $limit;");
}
 
// Select Query
function mSS($table,$select,$condition,$limit)
{
    return mysql_query("SELECT $select FROM $table WHERE $condition LIMIT $limit;");
}
 
// Update Query
mSU("users","online","0","username = 'howdy'","1");
 
// Select Query
mSS("users","id","username LIKE '%boobface%'","5");
 
?>
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Nice code but I prefer to use classes/objects when using MySQL now.

Also, you don't need that ';' after $limit.
 

xenogfx

New Member
Oct 24, 2011
24
12
Nice code but I prefer to use classes/objects when using MySQL now.

Also, you don't need that ';' after $limit.

the ; makes it so if you have multiple queries running at the same time they dont fuck up. :)
 

brsy

nah mang
May 12, 2011
1,530
272
Looks nice, but I prefer using classes sorry. I suggest making a TUT explaining what classes are for the MySQL/MySQLi newbies.
 

xenogfx

New Member
Oct 24, 2011
24
12
mysql_query ("SELECT $select FROM $table WHERE $condition LIMIT $limit; SELECT $select FROM $table WHERE $condition LIMIT $limit;");

^ what is that rasta..
Looks nice, but I prefer using classes sorry. I suggest making a TUT explaining what classes are for the MySQL/MySQLi newbies.

We know how it can be inserted into a class >_>

class Name {
myfunction //
myfunction //
}

how hard is it..
 

Xenous

o shi
Nov 15, 2011
383
101
For those of you who wanted it in a class for mysql,

class.mysql.php :
PHP:
class xSQL {
         
                # Initial Connection to MySQL will be done through global.php for dynamic reasons. #
 
    public function __construct($host, $username, $password, $db) {
     
        $xCon = mysql_connect($host, $username, $password) or die('MySQL Error - Could not connect to mysql');
        mysql_select_db($db, $xCon) or die('MySQL Error - Could not locate database');
  }
    function mSU($table,$column,$value,$condition,$limit)
  {
    mysql_query("UPDATE $table SET $column = '$value' WHERE $condition LIMIT $limit;");
  }
 
// Select Query
    function mSS($table,$select,$condition,$limit)
  {
    return mysql_query("SELECT $select FROM $table WHERE $condition LIMIT $limit;");
  }
}
global.php :
PHP:
require_once "inc/class.mysql.php";
 
    # Defining the classes. #
 
$db = new xSQL('localhost', 'root', 'password', 'database');
 
    # And xeno's example # 
$db->mSU("users","online","0","username = 'howdy'","1");
 
// Select Query
$db->mSS("users","id","username LIKE '%boobface%'","5");

Done in rough so don't flame if its wrong
 
Status
Not open for further replies.

Users who are viewing this thread

Top