Jump to content

[Help] mysql problem


aleksa.mta

Recommended Posts

Posted

When I try to connect my script to a local mysql database using

handler = mysql_connect("127.0.0.1", "root", "", "random_db") 

I get an error saying

attempt to call global mysql_connect (a nil value) 

There are only 2 English topic about this problem online and both are left unsolved. The data I entered is correct, I suppose, because I've read some tutorials and it says that I should use "127.0.0.1" as host, "root" as username and nothing as password, also the database name is correct.

All XAMPP modules running and I've downloaded mta_mysql.dll, put it in the modules folder and added

<module src="mta_mysql.dll" />  

in mtaserver.conf. I copied libmysql.dll in the server folder, as it says I should.. When I start the server it says that everything is running correctly.

So I hope someone can solve it, thanks

Posted

Did you put mta_mysql.dll in the "modules" folder?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

  • Moderators
Posted
Did you put mta_mysql.dll in the "modules" folder?
I've downloaded mta_mysql.dll, put it in the modules folder

So yeah I guess he did.

Try starting again your server and check the console to see something related to the mta_mysql module.

When everything is setting up correctly, it should show something like this:

[2014-02-17 20:56:07] MODULE: Loaded "MySQL 5.0 database module" (0.50) by "Alberto Alonso <[email protected]>" 
[2014-02-17 20:56:07] Starting resources... 

If the dlls aren't in the right folders:

[2013-11-09 23:58:11] MODULE: Unable to find modules/mta_mysql.dll! 
[2013-11-09 23:58:11] Starting resources... 

Regards,

Citizen

The rEvolution is coming ...

Posted

My bad, I didn't notice that part.

@aleksa.mta: You can always use the MTA built-on MySQL functions, they are quite easier to use, at least for me.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Make sure you put the script as server side in the meta.xml.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

  • MTA Team
Posted

Yes, script for that module must be Server-Side.

You can fix it using triggers to send and recive the data:

  
triggerClientEvent 
triggerServerEvent 

DevOps Engineer, Cloud Advocate & Security Engineer(Red Team) | Coffee, Containers & Burp

 
Posted

Thanks for the help but I have another problem. Say I do

  
local result = mysql_query (some command) 
  

And the command is functional since I tested it in phpMyAdmin. The command starts with SELECT so it's suppose to get a value from some column. Now my question is, if it's a single value, how do I convert it to string in order to use it further on because when I try to outputChatBox the value it says that it expected string at argument 1 and got userdata.

Posted

You must use mysql_fetch_assoc along with mysql_query if I'm right.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Post your code and tell me where is that error coming from.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

  • MTA Team
Posted

Probably you have to loop the table. Not sure, but post your code.

DevOps Engineer, Cloud Advocate & Security Engineer(Red Team) | Coffee, Containers & Burp

 
Posted

function checkForUsername (username) 
    handler = mysql_connect("127.0.0.1", "root", "", "akrp_db") 
    local result = mysql_query(handler, "SELECT USERNAME FROM `user_info` WHERE USERNAME = '" .. username .. "'") 
    outputChatBox (result) 
end 
  
addEvent("onCheckForUsername", true) 
addEventHandler ("onCheckForUsername", getRootElement(), checkForUsername) 
  

It's triggered from the client-side script, which I can't send you, too big.

error: http://prntscr.com/2th0wr

Posted

But I told you to use mysql_fetch_assoc, I can't see it on that code.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

  
function checkForUsername (username) 
    handler = mysql_connect("127.0.0.1", "root", "", "akrp_db") 
    local result = mysql_query(handler, "SELECT USERNAME FROM `user_info` WHERE USERNAME = '" .. username .. "'") 
    local res2 = mysql_fetch_assoc (result) 
    outputChatBox (res2) 
  
  
end 
  
addEvent("onCheckForUsername", true) 
addEventHandler ("onCheckForUsername", getRootElement(), checkForUsername) 
  
  

http://prntscr.com/2tjlk4

Posted

Keep in mind that your MySQL query is vulnerable to SQL injections right now.

If I helped you, please click the like button on the right ;) Thanks!

Posted

Instead of printing an array, print the username string by typing "res2.USERNAME", which should work.

If I helped you, please click the like button on the right ;) Thanks!

Posted

mysql_fetch_assoc returns a table, you have to use that table to return any string values, so, use the following.

  
function checkForUsername( username ) 
    handler = mysql_connect( "127.0.0.1", "root", "", "akrp_db" ) 
    local result = mysql_query( handler, "SELECT USERNAME FROM `user_info` WHERE USERNAME = '" .. username .. "'" ) 
    local res2 = mysql_fetch_assoc( result ) 
    outputChatBox( res2.USERNAME ) 
end 
addEvent( "onCheckForUsername", true ) 
addEventHandler( "onCheckForUsername", root, checkForUsername ) 

If I helped you, please click the like button on the right ;) Thanks!

Posted

What you said works, thank you.

But now I'm having another problem and hopefully the last one. So assume that res2.USERNAME is a nil value.

How do I make an if statement that will test if it's a nil value? Because when I do

local res3 = res2.USERNAME 
if res3 == nil then 
     outputChatBox ("Nil") 
end 

it says "attempt to index local res3 (a nil value)

So even if it is nil I want the way for the program to confirm it's nil.

EDIT: Actually the problem seems to be here that if res2 is nil, it can't event do res3.USERNAME ...

Posted
function checkForUsername ( username ) 
    handler = mysql_connect ( "127.0.0.1", "root", "", "akrp_db" ) 
    local result = mysql_query ( handler, "SELECT USERNAME FROM `user_info` WHERE USERNAME = '" .. username .. "'" ) 
    local res2 = mysql_fetch_assoc ( result ) 
    if ( type ( res2 ) == "table" ) then 
        outputChatBox ( res2.USERNAME ) 
    else 
        outputChatBox ( "NIL" ) 
    end 
end 
addEvent ( "onCheckForUsername", true ) 
addEventHandler ( "onCheckForUsername", root, checkForUsername ) 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...