Ads by ProfitSence

Functions in MySQL

Last Updated on Wednesday 5th Oct 2022


This initializes an MYSQL object that can be used in the mysql_real_connect() method.

Here is its syntax

					MYSQL *mysql_init(MYSQL *object)

  • If the object parameter that's passed is NULL, then the function initializes and returns a new object.
  • Otherwise, the supplied object is initialized, and the address of the object is returned.


This function closes the previously opened MySQL connection. Here is its syntax

					void mysql_close(MYSQL *mysqlObject)

  • It de-allocates the connection handler that's represented by the mysqlObject parameter.
  • The function returns no value.


This establishes a connection to a MySQL database engine running on the specified host. Here is its syntax.

					MYSQL *mysql_real_connect(MYSQL *mysqlObject, const char *hostName, const char *userid, const char *password, const char *dbase, unsigned int port, const char *socket, unsigned long flag)

  • mysqlObject represents the address of an existing MYSQL object.
  • hostName is where the hostname or IP address of the host is provided. To connect to localhost, either NULL or the string localhost is provided.
  • userid represents a valid MySQL login ID.
  • password represents the password of the user.
  • dbase represents the database name to which the connection has to be established.
  • port is where either value 0 is specified, or the port number for the TCP/IP connection is supplied.
  • socket is where either NULL is specified, or the socket or named pipe is supplied.
  • flag can be used to enable certain features, such as handling expired passwords and applying compression in the client/server protocol, but its value is usually kept at 0.


This frees the memory allocated to a result set. Here is its syntax.

					void mysql_free_result(MYSQL_RES *resultset)


Here, resultset represents the set whose memory we want to free up.


This function executes the supplied SQL query. Here is its syntax

					int mysql_query(MYSQL *mysqlObject, const char *sqlstmt)

  • Here: mysqlObject represents the MYSQL object sqlstmt represents the null-terminated string that contains the SQL statement to be executed.
  • The function returns 0 if the SQL statement executes successfully. Otherwise, it returns a non-zero value.


After the successful execution of an SQL statement, this method is used to save the result set. This means that the result set is retrieved and returned. Here is its syntax

					MYSQL_RES *mysql_use_result(MYSQL *mysqlObject)

  • Here, mysqlObject represents the connection handler.
  • If no error occurs, the function returns an MYSQL_RES result structure.
  • In case of any error, the function returns NULL.


This returns the number of values, that is, columns in the supplied row. Here is its syntax.

					unsigned int mysql_num_fields(MYSQL_ROW row)

  • Here, the parameter row represents the individual row accessed from the resultset.


This function fetches the next row from a result set. The function returns NULL if there are no more rows in the result set to retrieve or an error occurs. Here is its syntax.

					MYSQL_ROW mysql_fetch_row(MYSQL_RES *resultset)

  • Here, the resultset parameter is the set from which the next row must be fetched.
  • You can access values in the column of the row by using the subscript row[0], row[1], and so on, where row[0] represents the data in the first column, row[1] represents the data in the second column, and so on.