HEX
Server: LiteSpeed
System: Linux srv1.dhviews.com 5.14.0-570.23.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jun 24 11:27:16 EDT 2025 x86_64
User: bdedition (1723)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //proc/thread-self/root/proc/thread-self/root/usr/share/mysql/man/man3/mysql_select_db.3
.\" Automatically generated by Pandoc 2.5
.\"
.TH "mysql_select_db" "3" "" "Version 3.3.1" "MariaDB Connector/C"
.hy
.SS Name
.PP
mysql_select_db \- selects a database as default
.SS Synopsis
.IP
.nf
\f[C]
#include <mysql.h>

int mysql_select_db(MYSQL * mysql,
                    const char * db);
\f[R]
.fi
.SS Description
.PP
Selects a database as default.
Returns zero on success, non\-zero on failure
.SS Parameters
.IP \[bu] 2
\f[C]mysql\f[R] is a connection identifier, which was previously
allocated by \f[B]mysql_init(3)\f[R] and connected by
\f[B]mysql_real_connect(3)\f[R].
.IP \[bu] 2
\f[C]db\f[R] \- the default database name
.SS Notes
.IP \[bu] 2
To retrieve the name of the default database either execute the SQL
command \f[C]SELECT DATABASE()\f[R] or retrieve the value via
\f[B]mariadb_get_infov(3)\f[R] API function.
.IP \[bu] 2
The default database can also be set by the db parameter in
\f[B]mysql_real_connect(3)\f[R].
.SS Examples
.SS SQL
.IP
.nf
\f[C]
# switch to default database test
USE test;
# check default database
SELECT DATABASE();
+\-\-\-\-\-\-\-\-\-\-\-\-+
| database() |
+\-\-\-\-\-\-\-\-\-\-\-\-+
| test       |
+\-\-\-\-\-\-\-\-\-\-\-\-+
\f[R]
.fi
.SS MariadDB Connector/C
.IP
.nf
\f[C]
static int set_default_db(MYSQL *mysql)
{
  int rc;
  char *default_db;

  /* change default database to test */
  rc= mysql_select_db(mysql, \[dq]test\[dq]);
  if (rc)
    return rc;  /* Error */

  /* get the default database */
  rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SCHEMA, &default_db);
  if (rc)
    return rc; /* Error */

  if (strcmp(\[dq]test\[dq], default_db) != NULL)
  {
    printf(\[dq]Wrong default database\[rs]n\[dq]);
    return 1;
  }
  printf(\[dq]Default database: %s\[dq], default_db);
  return 0;
}
\f[R]
.fi
.SS See also
.PP
\f[B]mysql_real_connect(3)\f[R]