Easy options table class

<?php

/*
* HypOptionsTable Class
* Constructor: $new_hyper_options = new HypOptionsTable($table_name,$option_names);// Pass options names as an array
* Access the table: $hyper_options_ref = new HypOptionsTable($tablename);// No second argument = no table created
*
* For MU it is usefull to create a table with the prefix of the child blog, like wp_3_mytablename. These will be cleaned
* up when said child site is deleted from the MU interface.
*/

class HypOptionsTable{
private $_table_name;
private $_option_names;

public function __construct($table_name,$option_names = false){
if($table_name) $this->_table_name = $table_name;
if($option_names){
$this->_option_names = $option_names;
$this->create_table();
}
}
private function create_table(){
global $wpdb;
$table_name = $this->_table_name;
$option_names = $this->_option_names;
$db_version = '1.0';
if($wpdb->get_var("show tables like '$table_name'") != $table_name){
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
option_name VARCHAR(100) NOT NULL,
option_value TEXT NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('db_version',$db_version);
//Instert Tables
foreach($option_names as $name){
$wpdb->insert($table_name, array('option_name' => $name,'option_value' => ''));
}
}
}
public function addOption($option_name,$option_value){
global $wpdb;
$table_name = $this->_table_name;
if($wpdb->query("SELECT * FROM $table_name WHERE option_name = '$option_name'")) $this->updateOption($option_name,$option_value);
else $wpdb->insert($table_name, array('option_name' => $option_name,'option_value' => $option_value));
}
public function updateOption($option_name,$option_value){
global $wpdb;
$table_name = $this->_table_name;
$wpdb->update($table_name, array('option_value' => $option_value), array('option_name' => $option_name));
}
public function getOption($option_name){
global $wpdb;
$table_name = $this->_table_name;
$results = $wpdb->get_row("SELECT option_value FROM $table_name WHERE option_name = '$option_name'");
return $results->option_value;
}
public function deleteOption($option_name){
global $wpdb;
$table_name = $this->_table_name;
$wpdb->query("DELETE FROM $table_name WHERE option_name = '$option_name'");
}

}//End HypOptionsTable

/*
//Test Constructor, creates table
$option_name_array = array('username','email','password');
$new_hyper_options = new HypOptionsTable('wp_hyper_options',$option_name_array);

//Test Reference - New table not created due to lack of second argument
$hyper_options = new HypOptionsTable('wp_hyper_options');
$hyper_options->addOption('rudeness_level','wicked bad');

//Test Update
$hyper_options->updateOption('username','dippy');

//Test Get
echo $hyper_options->getOption('username');
*/

?>

Options table for DB

<?php

//Setup DB
function lmcb_create_db_table(){
global $wpdb;
$lmcb_db_version = "1.0";

$table_name = $wpdb->prefix . "band_info";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
option_name VARCHAR(150) NOT NULL,
option_value TEXT NOT NULL,
UNIQUE KEY id (id)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("lmcb_db_version", $lmcb_db_version);

//Band Options
$band_info_options = array('main_image','description');
foreach($band_info_options as $option){
$wpdb->insert($table_name, array('option_name' => $option, 'option_value' => ''));
}
}
}

add_action('init','lmcb_create_db_table');

function lmcb_to_band_info_db(){
global $wpdb;

$table_name = $wpdb->prefix . "band_info";
foreach($_POST as $key => $value){
$wpdb->update($table_name, array('option_value' => $value), array('option_name' => $key));
}
}

?>

Simple SQL table creation

<?php

global $test_db_version;
$test_db_version = "1.0";

function test_create_db_table() {
global $wpdb;
global $test_db_version;

$table_name = $wpdb->prefix . "test_table";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time varchar(11) NOT NULL,
title text NOT NULL,
body text NOT NULL,
UNIQUE KEY id (id)
);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);

add_option("test_db_version", $test_db_version);
}
}

function test_add_row(){
global $wpdb;
$table_name = $wpdb->prefix . "test_table";

$test_title = "The Title";
$test_body = "The Body Text";

$rows_affected = $wpdb->insert( $table_name, array( 'time' => date("m-d-Y"), 'title' => $test_title, 'body' => $test_body ) );
}

add_action('init','test_create_db_table');
/* Uncomment this action to initiate a new row
Just replcace wp_login with the hook of your choice
add_action('wp_login','test_add_row');
*/
?>

Convert IP to IP Number

$user_ip = explode(".", $_SERVER['REMOTE_ADDR']);

$w=$user_ip[0];
$x=$user_ip[1];
$y=$user_ip[2];
$z=$user_ip[3];

$ipnum = 16777216*$w + 65536*$x + 256*$y + $z;

//Beter Solution is to use the 'inet_aton' function in MySql
$the_ip = $_SERVER['REMOTE_ADDR'];
$country_query = "SELECT country_code2,country_name FROM webstats ".
"WHERE IP_FROM<=inet_aton('$the_ip') ".
"AND IP_TO>=inet_aton('$the_ip') ";

Database Constants

<?php

//Database Contants
defined('DB_SERVER') ? null : define("DB_SERVER","localhost");
defined('DB_NAME') ? null : define("DB_NAME","put_db_name_here");
defined('DB_USER') ? null : define("DB_USER","put_db_username_here");
defined('DB_PASS') ? null : define("DB_PASS","put_db_password_here");

?>

Gravity Forms file upload query

<?php

//Connect to Database
$gravity_post_id = get_the_ID();

$dbc = mysqli_connect('localhost','dankuniv_wp02','H0fx81McyP','dankuniv_wp02')
or die('Error connecting to Database');

//This query gets the secondary ID
$query = "SELECT id
FROM wp_rg_lead
WHERE post_id = $gravity_post_id
LIMIT 0 , 30";

$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);

//The secondary ID from above
$secondary_id = $row[0];

if ($row[0] != ''){

//This query grabs the file URL
$query2 = "SELECT value
FROM wp_rg_lead_detail
WHERE lead_id = $secondary_id
AND field_number = 3
LIMIT 0 , 200";

$result2 = mysqli_query($dbc, $query2);
$row2 = mysqli_fetch_array($result2);

//Create Source file link:
if ($row2[0] == ''){
//Empty field - exit conditional;
}
else {
echo '<a href="' . $row2[0] . '"><img src="/graphics/download_source.png" /></a>';
}
}

//Close the Connection
mysqli_close($dbc);

?>

SQL Query

<?php

$dbc = mysqli_connect('localhost','the_database_user','the_database_password','the_database_name') or die('Error connecting to Database');

$query = "SELECT *
FROM table_name
WHERE id = 1
LIMIT 0 , 30";

$result = mysqli_query($dbc, $query);

$row = mysqli_fetch_array($result);

echo $row[0];

//Close the Connection
mysqli_close($dbc);

?>