Jump to content

[Help] mysql problem


aleksa.mta

Recommended Posts

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

Link to comment
  • Moderators
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

Link to comment

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.

Link to comment

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

Link to comment

  
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

Link to comment

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 ) 

Link to comment

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 ...

Link to comment
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 ) 

Link to comment

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...