Forum Moderators: coopster

Message Too Old, No Replies

From MySQL separating column data into different variables for output

         

Dreap

11:39 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error()); mysql_select_db("$dbname") or die(mysql_error());
$username = 'masta' ;

$query5 = mysql_query("SELECT * FROM rtp_reg WHERE db_key LIKE '%$username%'") or die(mysql_error());
if(mysql_num_rows($query5)) {
}

$querye = "SELECT * FROM `rtp_reg` WHERE `db_key` = 'masta'";
$result = mysql_query($querye) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['data'] . "<br/>";
}


Currently it replies me with
dumdi3da 2113687 49 350 1220 175 255 400 270 0 180 100 40 735 100


I need to get it separate all values and loose first one entirely in output.
For example


$notgonnausethisvariable = 'dumdi3da';
$totalscore = '2113687';
$extreme = '49'
$extra = '350'
$crowded = '1220'
.. so on.


Then I could use echo to output data I like for public.

I tried something like this but it didn't work

list($totalscore, $extreme, $extra, $crowded) = split('[ ]', $row['data']);

$new_string = preg_replace('/[^a-z0-9\s]/i', '', $totalscore);
$new_string2 = preg_replace('/[^a-z0-9\s]/i', '', $extreme);
$new_string3 = preg_replace('/[^a-z0-9\s]/i', '', $extra);
$new_string4 = preg_replace('/[^a-z0-9\s]/i', '', $crowded);


So what I need is to get variables for each value in MySQL column "data" to use them for output via echo.

mack

3:40 am on Mar 17, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Depending on how much data you already have I think the easiest option is to re-structure your database to have a column for each piece of data.

You could then use something like the following...

$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array( $result ))
{
$something = $row['something'];
$thing = $row['thing'];
$this = $row['this'];
$that = $row['that'];
}


You could then echo them where needed.

Mack.

Dreap

6:05 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Then I would have to change following parts of script that saves into mysql. What should I change here for it to start saving as you saying?

Parts that have mysql in..

#define SAVEDB_MYSQL 2

}else if(g_savedb==SAVEDB_MYSQL){
for(new i=1; i<6; i++){
format(cache, 127, "drop table if exists %s;", g_saveby_name[i])
SQL_ThreadQuery(g_sql_tuple, "handle_change_table_query", cache)
}
}else if(g_savedb==SAVEDB_MYSQL){
for(new i=1; i<6; i++){
format(cache, 127, "RENAME TABLE %s TO %s_%s;", g_saveby_name[i], temp, g_saveby_name[i])
SQL_ThreadQuery(g_sql_tuple, "handle_change_table_query", cache)
}

case SAVEDB_MYSQL: sql_init()
fputs(fh, "rpgm_savedb 1^t^t^t//1 = nVault, 2 = MySql^n^n")

}else{ // g_savedb == SAVEDB_MYSQL
formatex(cache, 511, "REPLACE INTO %s (db_key, data) VALUES ('%s', '%s');", g_saveby_name[saveby], key, data)
new temp[2]
temp[0] = id
SQL_ThreadQuery(g_sql_tuple, "handle_sql_save", cache, temp, 2)
}
if(saveby == SAVEBY_LOGIN){
data_delete(id, g_saveby)
}


}else{ // g_savedb == SAVEDB_MYSQL
data[0] = id
data[1] = i
g_p_data_loaded[id] = DATA_LOADING
format(cache, 255, "SELECT data FROM %s WHERE db_key = '%s'", g_saveby_name[saveby], key)
SQL_ThreadQuery(g_sql_tuple, "handle_sql_load", cache, data, 2)
}

}


public handle_sql_load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
static id, i, cache[256]
if(g_db_status != DB_READY)
return PLUGIN_HANDLED
if(FailState){
log_amx("SQL Error: %s (%d)", Error, Errcode)
return PLUGIN_HANDLED
}

id = Data[0]
i = Data[1]
if(SQL_MoreResults(Query)){
SQL_ReadResult(Query, 0, cache, 255)
decode_data_str(id, cache, i)
}else{
//first time connect
g_p_data_loaded[id] = DATA_NOT_FOUND
if(g_p_login_status[id] == REG_TRY){
g_p_login_status[id] = REG_DONE
data_save(id) //lets reserve this username
client_print(id, print_chat, "[RPG mod] You have successfully registered")
client_print(id, print_console, "[RPG mod] You have successfully registered")
g_p_pw_hidden[id] = true
set_default_lvl_upg(id)
}
}
return PLUGIN_CONTINUE
}

}else{ // g_savedb == SAVEDB_MYSQL
data[0] = id
data[1] = i
g_p_data_loaded[id] = DATA_LOADING
format(cache, 255, "SELECT data FROM %s WHERE db_key = '%s'", g_saveby_name[saveby], key)
SQL_ThreadQuery(g_sql_tuple, "handle_sql_load", cache, data, 2)
}

}


public handle_sql_load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
static id, i, cache[256]
if(g_db_status != DB_READY)
return PLUGIN_HANDLED
if(FailState){
log_amx("SQL Error: %s (%d)", Error, Errcode)
return PLUGIN_HANDLED
}

id = Data[0]
i = Data[1]
if(SQL_MoreResults(Query)){
SQL_ReadResult(Query, 0, cache, 255)
decode_data_str(id, cache, i)
}else{
//first time connect
g_p_data_loaded[id] = DATA_NOT_FOUND
if(g_p_login_status[id] == REG_TRY){
g_p_login_status[id] = REG_DONE
data_save(id) //lets reserve this username
client_print(id, print_chat, "[RPG mod] You have successfully registered")
client_print(id, print_console, "[RPG mod] You have successfully registered")
g_p_pw_hidden[id] = true
set_default_lvl_upg(id)
}
}
return PLUGIN_CONTINUE
}


public decode_data_str(id, data[], i){
static temp[32], num
for(; i<MAX_UPGS; i++){
strbreak(data, temp, 31, data, 255)
remove_quotes(temp)
switch(i){
case -3:{
if(!equal(temp, g_p_password[id])){
client_print(id, print_chat, "[RPG mod]Error: username found in database, incorrect password.")
client_print(id, print_console, "[RPG mod]Error: username found in database, incorrect password.")
g_p_username[id] = ""
g_p_password[id] = ""
g_p_login_status[id] = REG_NONE
g_p_data_loaded[id] = DATA_LOADED
set_default_lvl_upg(id)
return
}
client_print(id, print_chat, "[RPG mod] You have successfully logged in")
client_print(id, print_console, "[RPG mod] You have successfully logged in")
g_p_pw_hidden[id] = true
}


public plugin_end(){
data_save_all()
if(g_db_status == DB_READY && g_savedb == SAVEDB_MYSQL)
SQL_FreeHandle(g_sql_connection)
}

[edited by: eelixduppy at 6:18 pm (utc) on Mar 17, 2010]
[edit reason] snipped URL [/edit]