Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-19-2007, 01:45 AM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
Custom SQL Functions

PHP-File [sql.php]:
PHP Code:
<?php

$mysql_host 
= $_GET['host'];
$mysql_username = $_GET['user'];
$mysql_password = $_GET['pass'];
$mysql_db = $_GET['db'];
$mysql_query = stripslashes($_GET['query']);

$SQLConnect = mysql_connect($mysql_host, $mysql_username, $mysql_password);

if (!
$SQLConnect)
{
  die (
'Could not connect:' . mysql_error());
}

mysql_selectdb($mysql_db, $SQLConnect);

$SQLQuery = mysql_query($mysql_query);

if (!
$SQLQuery)
{
  die (
'Could not follow through with query: ' . mysql_error());
}

  echo(
'Query Succesful: ' . $mysql_query);

  
mysql_close($SQLConnect);

?>
Class [sql_settings]:
PHP Code:
/*
DEFAULT SETTINGS FOR mySQL
*/
function onCreated()
{
  
this.sql = new TStaticVar();
  
this.sql.connectfile = "http://site.tld/dir/to/sql.php"; // Directory of: sql.php
  
this.sql.host = "localhost"; // Your SQL host [Usually 'localhost']
  
this.sql.db = ""; // The database you wish to work with
  
this.sql.username = ""; // The SQL-DB username
  
this.sql.password = ""; // The SQL-DB password
} 
Class [sql_functions]:
PHP Code:
function SQL_Update(table, field, newvalue, row, value)
{
  
temp.query = format("UPDATE `%s` SET `%s` = '%s' WHERE `%s` = %s", temp.table, temp.field, temp.newvalue, temp.row, temp.value);
  return 
temp.query;
}

function 
SQL_Delete(table, field, value)
{
  
temp.query = format("DELETE FROM `%s` WHERE `%s` = '%s';", temp.table, temp.field, temp.value);
  return 
temp.query;
}

function 
SQL_Insert(table, field, value)
{
  if ((
temp.field.size() == temp.value.size()) && temp.field.size() > 1)
  {
    
temp.query = format("INSERT INTO `%s` (", temp.table);
    
    for (
i = 0; i < temp.field.size(); i ++)
    {
      
temp.query @= "`" @ temp.field[i] @ "`";
      if (
i < temp.field.size()-1)
        
temp.query @= " , ";
    }
    
    
temp.query @= ") VALUES (";
    
    for (
j = 0; j < temp.value.size(); j++)
    {
      
temp.query @= "'" @ temp.value[j] @ "'";
      if (
j < temp.value.size()-1)
        
temp.query @= " , ";
    }
    
    
temp.query @= ");";
    return 
temp.query;
    
  } else {
    
temp.query = format("INSERT INTO `%s` (`%s`) VALUES ('%s')", temp.table, temp.field, temp.value);
    return 
temp.query;
  }
}

function 
RequestSQL(host, username, password, query)
{
  
temp.url = format("%s?host=%s&user=%s&pass=%s&db=%s&query=%s", this.sql.connectfile, this.sql.host, this.sql.username, this.sql.password, this.sql.db, temp.query);
  
temp.req = requesturl(temp.url);
  
this.catchevent(temp.req, "onReceiveData", "onSQLReceived");
}


function 
onSQLReceived(page)
{
  
temp.returnData = page.data[0];
  for (
p: allplayers)
  {
    if (
p == "rapidwolve")    // RC Player
      
p.sendpm(temp.returnData);
  }
} 
------------

Example DB-NPC [SQL]:
PHP Code:
/*
  This is an example assuming our SQL settings class is configured properly.
*/

function onCreated()
{
  
this.join("sql_settings");
  
this.join("sql_functions");
  
RegisterMember("rapidwolve", "[email protected]");
}

function 
RegisterMember(member, email)
{
  
temp.query = SQL_Insert("members", {"Account", "Email"}, {temp.member, temp.email});
  
RequestSQL(this.sql.host, this.sql.username, this.sql.password, temp.query);
} 

Last edited by Rapidwolve; 05-19-2007 at 02:12 AM..
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.