SAMP-MySQL



  Page en Français

SAMP-MySQL is a plugin designed for sa:mp v0.3 wich allows access to a MySQL server.
This plugin is provided freely with sourcecode.
You are free to use it on your own server.
If you are interrested to participate to development or having some suggestions, you can contact the creator directly on the official forum of SA:MP :
http://forum.sa-mp.com/index.php?topic=23931.0

Download
  • Latest release: SAMP-MySQL v0.15 Pawn Kit (mirror)

  • This release includes: PAWN .inc file (to be placed in "include" dir in pawno dir) and .pwn example file.

  • Latest release: SAMP-MySQL v0.15 Windows (mirror)

  • This release includes: Windows .dll file and source.

  • Latest release: SAMP-MySQL v0.15 Ubuntu and debian-based linux (mirror)

  • This release includes: Linux .so file, libmysqlclient.so for Ubuntu and source.

  • Latest release: SAMP-MySQL v0.15 Centos and RedHat-based linux (mirror)

  • This release includes: Linux .so file, libmysqlclient.so for Centos and source.

    Index functions
  • samp_mysql_connect()

  • samp_mysql_select_db()

  • samp_mysql_query()

  • samp_mysql_store_result()

  • samp_mysql_fetch_row()

  • samp_mysql_get_field()

  • samp_mysql_num_rows()

  • samp_mysql_num_fields()

  • samp_mysql_ping()

  • samp_mysql_real_escape_string()

  • samp_mysql_free_result()

  • samp_mysql_strtok()

  • samp_mysql_close()



  • This function allows you to connect to a database server. This is the first call: it initialize the plugin and etablish the connection.
    Entries:
  • server: IP address of the server.

  • user: username to access the database server.

  • password: password corresponding to user.

  • Returns:
  • 0: if failure.

  • 1: if success.

  • Example:

    ...
    samp_mysql_connect("127.0.0.1", "root", "");
    ...
    This function allows you to select the database.
    Entries:
  • db: name of the database.

  • Returns:
  • 0: if failure.

  • 1: if success.

  • Example:

    ...
    samp_mysql_select_db("test");
    ...
    This function allows you to execute a query: SELECT, INSERT, UPDATE, DELETE, DROP, ALTER,...
    Entries:
  • query: query to execute.

  • Returns:
  • 0: if failure.

  • 1: if success.

  • Example:

    ...
    samp_mysql_query("SELECT * FROM users");
    ...
    This function prepare result to be processed. It must be called only if you made a SELECT query.
    Entries:
  • nothing

  • Returns
  • 0: if failure.

  • 1: if success.

  • Example:

    ...
    samp_mysql_store_result();
    ...
    This function allows you to get a line of result.
    Entries:
  • line: it's the variable that will contain a line of result.

  • Returns:
  • line: returns a line of result: fields are seperated by '|'. Ex: "1|toto|mypassword|toto@toto.com". If 1 field is NULL (SQL value) a string "NULL" will be returned for the NULL field. If 1 field value is empty, an empty string is returned for the empty field.

  • 0: if there's no more lines to get.

  • 1: if success.

  • Example:

    ...
    new line[1024];
    samp_mysql_fetch_row(line);
    ...
    This function allows you to get a value of a field.
    It must be called after samp_mysql_fetch_row() !
    Entries:
  • field: it's the field name.

  • value: it's the variable that will contain the fieldvalue.

  • Returns:
  • value: returns the value of the field: if field is NULL (SQL value) a string "NULL" will be returned. If field value is empty, an empty string is returned.

  • 0: Nothing was found (maybe the fieldname is mispelled).

  • 1: If something is found.

  • Example:

    ...
    new line[1024];
    new fieldvalue[128];
    samp_mysql_fetch_row(line);
    samp_mysql_get_field("user_name", field);
    printf("%s ", field);
    ...
    This function returns the number of result lines.
    Entries:
  • nothing
  • Returns:
  • int: the number of result lines.

  • Example:

    ...
    new num_line;
    num_line=samp_mysql_num_rows();
    ...
    This function returns the number of fields in the result.
    Entries:
  • nothing

  • Returns:
  • int: the number of fields in the result.

  • Example:

    ...
    new num_columns;
    num_columns=samp_mysql_num_fields();
    ...
    This function allows you to check the connection to the database server and make a re-connection if needed.
    Entries:
  • nothing

  • Returns:
  • 0: if success.

  • 1: if server is not contactable (connection lost).

  • 2: unknown error.

  • Example:

    ...
    if(samp_mysql_ping()==0)
    {
    printf("Connection to database is alive ! ");
    }
    ...


    This function allows you to prepare a string to be passed as a query. This function escapes all special chars to make string suitable to be used by MySQL engine.
    Entries:
  • src: source string (to be escaped).

  • dest: destination string (escaped string). dest_length must srclength*2+1

  • Returns:
  • int: length of the processed string.

  • Example:

    ...
    new src[128];
    new dest[257];
    //put something in src...
    samp_mysql_real_escape_string(src, dest);
    ...


    This function allows you to free allocated memory to a query result.
    Entries:
  • nothing

  • Returns:
  • 1: returns always 1.

  • Example:

    ...
    samp_mysql_free_result();
    ...


    This function allows you to split a string as strtok() C function.
    Entries:
  • dest: destination string (one part of the src string).

  • separator: string separator to split string.

  • src: source string (to be splitted). Use it just at the first call.

  • "": source string (to be splitted). Use it from the 2nd call to the end splitting process.

  • Returns:
  • 0: if this is the end of the string. Call it another time after returns 0, crash the server.

  • 1: if it "returns" string.

  • Example:

    ...
    new resultline[1024];
    new field[128];
    ...
    samp_mysql_fetch_row(resultline);
    samp_mysql_strtok(field, "|", resultline); //first call of strtok MUST contain resultline
    printf("%s ", field);
    while(samp_mysql_strtok(field, "|", "")==1) //From 2nd call to the end you MUST call strtok without resultline
    {
       printf("%s ", field);
    }
    ...


    This function allows you to close the connection to the database server.
    Entries:
  • nothing

  • Returns:
  • 0: if failure.

  • 1: if success.

  • Example:

    ...
    samp_mysql_close();
    ...