17/06/2017, 11:13 PM
hola quiero saber como conectarme a un base de datos local con wamp
tengo uno sma pero no conecta a mysql local
sacado de Aqui
tengo uno sma pero no conecta a mysql local
sacado de Aqui
Código PHP:
#include <amxmodx>
#include <sqlx>
#define PLUGIN "Estadisticas"
#define VERSION "1.0"
#define AUTHOR "3lmack'"
new g_exp[33], g_deaths[33];
new g_name[33][32]
new conectado[12]
#define DATA_BASE "server_cs"
new DB_HOST[] = "localhost"
new DB_ROOT[] = "root"
new DB_PASS[] = "pocaria"
new Handle:g_SQL_Tuple, Handle:g_SQL_Connection;
const TASK_MAKE_HUD = 5700;
new g_maxplayers, g_sync
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_logevent("Round_Ended", 2, "1=Round_End")
register_event("DeathMsg", "Event_Killed", "a")
set_task(0.1, "plugin_sqlx")
g_maxplayers = get_maxplayers()
g_sync = CreateHudSyncObj()
}
public plugin_end() {
SQL_FreeHandle(g_SQL_Tuple);
SQL_FreeHandle(g_SQL_Connection);
}
public HUD(taskid)
{
new id = taskid - TASK_MAKE_HUD
set_hudmessage(85, 170, 255, 0.6, 0.2, 0, 6.0, 1.1)
ShowSyncHudMsg(id, g_sync, "[Exp: %d || Muertes: %d]", g_exp[id], g_deaths[id])
}
public client_putinserver(id)
{
get_user_name(id, g_name[id], charsmax(g_name[]))
g_exp[id] = 0;
g_deaths[id] = 0;
conectado[id] = 1
load(id)
set_task(1.1, "HUD", id+TASK_MAKE_HUD, _, _, "b")
}
public client_disconnect(id)
{
conectado[id] = 0
save(id)
}
public Round_Ended()
{
new i;
for(i = 0; i < g_maxplayers; i++)
{
if(is_user_connected(i))
{
save(i)
}
}
}
public Event_Killed()
{
new attacker = read_data(1)
new victim = read_data(2)
if(victim == attacker)
{
g_exp[victim]--
g_deaths[victim]++
}
if(is_user_alive(attacker))
{
g_exp[attacker]++
g_deaths[victim]++
}
}
public plugin_sqlx()
{
new get_type[12]
SQL_SetAffinity("sqlite")
SQL_GetAffinity(get_type, sizeof(get_type))
if (!equali(get_type, "sqlite"))
{
log_to_file("SQLX.log", "Driver no encontrado")
pause("a")
}
else
{
static error, szError[300]
g_SQL_Tuple = SQL_MakeDbTuple(DB_HOST, DB_ROOT, DB_PASS, DATA_BASE)
g_SQL_Connection = SQL_Connect(g_SQL_Tuple, error, szError, 300)
if (strlen(szError))
{
log_to_file("ErrorSQL.log", szError)
pause("a")
}
CheckTabla()
}
}
public CheckTabla()
{
new Handle:sqlQuery;
sqlQuery = SQL_PrepareQuery(g_SQL_Connection, "CREATE TABLE IF NOT EXISTS 'Datos' (Nombre varchar(33) NOT NULL default '' PRIMARY KEY, Exp int NOT NULL default '0', Muertes int NOT NULL default '0', Conectado int NOT NULL default '0')")
if(!SQL_Execute(sqlQuery)) {
executeQuery(0, sqlQuery);
} else {
SQL_FreeHandle(sqlQuery);
}
}
public save(id)
{
new Handle:sqlQuery;
sqlQuery = SQL_PrepareQuery(g_SQL_Connection, "UPDATE Datos SET Exp = '%d', Muertes = '%d', Conectado = '%d' WHERE Nombre = ^"%s^";", g_exp[id], g_deaths[id], g_name[id], conectado[id])
if(!SQL_Execute(sqlQuery)) {
executeQuery(id, sqlQuery);
} else {
SQL_FreeHandle(sqlQuery);
}
}
public load(id)
{
new Handle:sqlQuery;
sqlQuery = SQL_PrepareQuery(g_SQL_Connection, "SELECT Exp, Muertes FROM 'Datos' WHERE Nombre = ^"%s^";", g_name[id])
sqlQuery = SQL_PrepareQuery(g_SQL_Connection, "UPDATE Datos SET Conectado = '%d' WHERE Nombre = ^"%s^"", conectado[id], g_name[id])
if(!SQL_Execute(sqlQuery)) {
executeQuery(id, sqlQuery);
} else if(SQL_NumResults(sqlQuery)) {
g_exp[id] = SQL_ReadResult(sqlQuery, 0);
g_deaths[id] = SQL_ReadResult(sqlQuery, 1);
SQL_FreeHandle(sqlQuery);
client_print(id, print_chat, "%s Tus datos fueron Cargados con Exito - Exp %d - Muertes %d", g_name[id], g_exp[id], g_deaths[id])
} else {
SQL_FreeHandle(sqlQuery);
sqlQuery = SQL_PrepareQuery(g_SQL_Connection, "INSERT INTO 'Datos' VALUES(^"%s^",'0','0', '1');", g_name[id])
if(!SQL_Execute(sqlQuery)) {
executeQuery(id, sqlQuery);
} else {
SQL_FreeHandle(sqlQuery);
}
}
}
public executeQuery(const id, const Handle:query) {
new sError[512];
SQL_QueryError(query, sError, 511);
log_to_file("logs_querys.txt", "- LOG: %s", sError);
if(is_user_connected(id)) {
server_cmd("kick #%d ^"Hubo un error al cargar/guardar tus datos. Intente nuevamente^"", get_user_userid(id));
}
SQL_FreeHandle(query);
}