aleksa.mta Posted February 17, 2014 Share Posted February 17, 2014 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
Castillo Posted February 17, 2014 Share Posted February 17, 2014 Did you put mta_mysql.dll in the "modules" folder? Link to comment
Moderators Citizen Posted February 17, 2014 Moderators Share Posted February 17, 2014 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
Castillo Posted February 17, 2014 Share Posted February 17, 2014 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. Link to comment
aleksa.mta Posted February 17, 2014 Author Share Posted February 17, 2014 http://prntscr.com/2tetro All good there.. Link to comment
Castillo Posted February 17, 2014 Share Posted February 17, 2014 Make sure you put the script as server side in the meta.xml. Link to comment
aleksa.mta Posted February 17, 2014 Author Share Posted February 17, 2014 Wait, those SQL functions are serverside? Cause my script is client-sided, eh Link to comment
MTA Team 0xCiBeR Posted February 17, 2014 MTA Team Share Posted February 17, 2014 Yes, script for that module must be Server-Side. You can fix it using triggers to send and recive the data: triggerClientEvent triggerServerEvent Link to comment
aleksa.mta Posted February 17, 2014 Author Share Posted February 17, 2014 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
Castillo Posted February 17, 2014 Share Posted February 17, 2014 You must use mysql_fetch_assoc along with mysql_query if I'm right. Link to comment
aleksa.mta Posted February 17, 2014 Author Share Posted February 17, 2014 Now it says 'expected string got table' Link to comment
Castillo Posted February 17, 2014 Share Posted February 17, 2014 Post your code and tell me where is that error coming from. Link to comment
MTA Team 0xCiBeR Posted February 17, 2014 MTA Team Share Posted February 17, 2014 Probably you have to loop the table. Not sure, but post your code. Link to comment
aleksa.mta Posted February 17, 2014 Author Share Posted February 17, 2014 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
Castillo Posted February 18, 2014 Share Posted February 18, 2014 But I told you to use mysql_fetch_assoc, I can't see it on that code. Link to comment
aleksa.mta Posted February 18, 2014 Author Share Posted February 18, 2014 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
myonlake Posted February 18, 2014 Share Posted February 18, 2014 Keep in mind that your MySQL query is vulnerable to SQL injections right now. Link to comment
aleksa.mta Posted February 18, 2014 Author Share Posted February 18, 2014 This is a coding problem, I need a simple explanation, warning me about something else won't help.. Link to comment
myonlake Posted February 18, 2014 Share Posted February 18, 2014 Instead of printing an array, print the username string by typing "res2.USERNAME", which should work. Link to comment
aleksa.mta Posted February 18, 2014 Author Share Posted February 18, 2014 Wait, I don't understand, can you just tell me how to store res2 into a string, for example called res3? Link to comment
myonlake Posted February 18, 2014 Share Posted February 18, 2014 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
aleksa.mta Posted February 18, 2014 Author Share Posted February 18, 2014 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
Castillo Posted February 18, 2014 Share Posted February 18, 2014 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now