<HTML ><HEAD ><TITLE >MySQL Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Function Reference" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="msql" HREF="function.msql.html"><LINK REL="NEXT" TITLE="mysql_affected_rows" HREF="function.mysql-affected-rows.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.msql.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.mysql-affected-rows.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.mysql" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >LXIII. MySQL Functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN57984" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.intro" ></A >Introduction</H1 ><P > These functions allow you to access MySQL database servers. More information about MySQL can be found at <A HREF="http://www.mysql.com/" TARGET="_top" >http://www.mysql.com/</A >. </P ><P > Documentation for MySQL can be found at <A HREF="http://www.mysql.com/documentation/" TARGET="_top" >http://www.mysql.com/documentation/</A >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.requirements" ></A >Requirements</H1 ><P > In order to have these functions available, you must compile PHP with MySQL support. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.installation" ></A >Installation</H1 ><P > By using the <TT CLASS="option" >--with-mysql[=DIR]</TT > configuration option you enable <TT CLASS="literal" >PHP</TT > to access MySQL databases. If you use this option without specifying the path to MySQL, <TT CLASS="literal" >PHP</TT > will use the built-in MySQL client libraries. With PHP 4 MySQL support is always enabled; if you don't specify the configure option, the bundled libraries are used. Users who run other applications that use MySQL (for example, running PHP 3 and PHP 4 as concurrent apache modules, or auth-mysql) should always specify the path to MySQL: <TT CLASS="option" >--with-mysql=/path/to/mysql</TT >. This will force <TT CLASS="literal" >PHP</TT > to use the client libraries installed by MySQL, avoiding any conflicts. </P ><P > The windows version of <TT CLASS="literal" >PHP</TT > has built in support for this extension. You do not need to load any additional extension in order to use these functions.</P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Crashes and startup problems of <TT CLASS="literal" >PHP</TT > may be encountered when loading this extension in conjunction with the recode extension. See the <A HREF="ref.recode.html" >recode</A > extension for more information. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.configuration" ></A >Runtime Configuration</H1 ><P > The behaviour of these functions is affected by settings in <TT CLASS="filename" >php.ini</TT >. </P ><P > <DIV CLASS="table" ><A NAME="AEN58013" ></A ><P ><B >Table 1. MySQL Configuration Options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Name</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Default</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >Changeable</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.allow_persistent</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"On"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.max_persistent</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"-1"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.max_links</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"-1"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.default_port</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.default_socket</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.default_host</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.default_user</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.default_password</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >NULL</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_ALL</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >mysql.connect_timeout</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >"0"</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >PHP_INI_SYSTEM</TD ></TR ></TBODY ></TABLE ></DIV > For further details and definition of the PHP_INI_* constants see <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >. </P ><P > Here is a short explanation of the configuration directives. <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><A NAME="ini.mysql.allow-persistent" ></A ><TT CLASS="parameter" ><I >mysql.allow_persistent</I ></TT > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Whether to allow <A HREF="features.persistent-connections.html" >persistent connections</A > to MySQL. </P ></DD ><DT ><A NAME="ini.mysql.max-persistent" ></A ><TT CLASS="parameter" ><I >mysql.max_persistent</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > The maximum number of persistent MySQL connections per process. </P ></DD ><DT ><A NAME="ini.mysql.max-links" ></A ><TT CLASS="parameter" ><I >mysql.max_links</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > The maximum number of MySQL connections per process, including persistent connections. </P ></DD ><DT ><A NAME="ini.mysql.default-port" ></A ><TT CLASS="parameter" ><I >mysql.default_port</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > The default TCP port number to use when connecting to the database server if no other port is specified. If no default is specified, the port will be obtained from the <TT CLASS="literal" >MYSQL_TCP_PORT</TT > environment variable, the <TT CLASS="literal" >mysql-tcp</TT > entry in <TT CLASS="filename" >/etc/services</TT > or the compile-time <TT CLASS="literal" >MYSQL_PORT</TT > constant, in that order. Win32 will only use the <TT CLASS="literal" >MYSQL_PORT</TT > constant. </P ></DD ><DT ><A NAME="ini.mysql.default-socket" ></A ><TT CLASS="parameter" ><I >mysql.default_socket</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > The default socket name to use when connecting to a local database server if no other socket name is specified. </P ></DD ><DT ><A NAME="ini.mysql.default-host" ></A ><TT CLASS="parameter" ><I >mysql.default_host</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > The default server host to use when connecting to the database server if no other host is specified. Doesn't apply in <A HREF="features.safe-mode.html#ini.safe-mode" >safe mode</A >. </P ></DD ><DT ><A NAME="ini.mysql.default-user" ></A ><TT CLASS="parameter" ><I >mysql.default_user</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > The default user name to use when connecting to the database server if no other name is specified. Doesn't apply in <A HREF="features.safe-mode.html#ini.safe-mode" >safe mode</A >. </P ></DD ><DT ><A NAME="ini.mysql.default-password" ></A ><TT CLASS="parameter" ><I >mysql.default_password</I ></TT > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > The default password to use when connecting to the database server if no other password is specified. Doesn't apply in <A HREF="features.safe-mode.html#ini.safe-mode" >safe mode</A >. </P ></DD ><DT ><A NAME="ini.mysql.connect-timeout" ></A ><TT CLASS="parameter" ><I >mysql.connect_timeout</I ></TT > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > Connect timeout in seconds. On Linux this timeout is also used for waiting for the first answer from the server. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.resources" ></A >Resource Types</H1 ><P > There are two resource types used in the MySQL module. The first one is the link identifier for a database connection, the second a resource which holds the result of a query. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.constants" ></A >Predefined Constants</H1 ><P > The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime. </P ><P > Since PHP 4.3.0 it is possible to specify additional client flags for the <A HREF="function.mysql-connect.html" ><B CLASS="function" >mysql_connect()</B ></A > and <A HREF="function.mysql-pconnect.html" ><B CLASS="function" >mysql_pconnect()</B ></A > functions. The following constants are defined: <DIV CLASS="table" ><A NAME="AEN58133" ></A ><P ><B >Table 2. MySQL client constants</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >constant</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >description</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >MYSQL_CLIENT_COMPRESS</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >use compression protocol</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >MYSQL_CLIENT_IGNORE_SPACE</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >Allow space after function names</TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >MYSQL_CLIENT_INTERACTIVE</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" >Allow interactive_timeout seconds (instead of wait_timeout) of inactivity before closing the connection.</TD ></TR ></TBODY ></TABLE ></DIV > </P ><P > The function <A HREF="function.mysql-fetch-array.html" ><B CLASS="function" >mysql_fetch_array()</B ></A > uses a constant for the different types of result arrays. The following constants are defined: <DIV CLASS="table" ><A NAME="AEN58152" ></A ><P ><B >Table 3. MySQL fetch constants</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH ALIGN="LEFT" VALIGN="MIDDLE" >constant</TH ><TH ALIGN="LEFT" VALIGN="MIDDLE" >description</TH ></TR ></THEAD ><TBODY ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >MYSQL_ASSOC</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > Columns are returned into the array having the fieldname as the array index. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >MYSQL_BOTH</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > Columns are returned into the array having both a numerical index and the fieldname as the array index. </TD ></TR ><TR ><TD ALIGN="LEFT" VALIGN="MIDDLE" >MYSQL_NUM</TD ><TD ALIGN="LEFT" VALIGN="MIDDLE" > Columns are returned into the array having a numerical index to the fields. This index starts with 0, the first field in the result. </TD ></TR ></TBODY ></TABLE ></DIV > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="mysql.examples" ></A >Examples</H1 ><P > This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN58172" ></A ><P ><B >Example 1. MySQL extension overview example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php /* Connecting, selecting database */ $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect"); print "Connected successfully"; mysql_select_db("my_database") or die("Could not select database"); /* Performing SQL query */ $query = "SELECT * FROM my_table"; $result = mysql_query($query) or die("Query failed"); /* Printing results in HTML */ print "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="function.mysql-affected-rows.html" >mysql_affected_rows</A > -- Get number of affected rows in previous MySQL operation</DT ><DT ><A HREF="function.mysql-change-user.html" >mysql_change_user</A > -- Change logged in user of the active connection </DT ><DT ><A HREF="function.mysql-character-set-name.html" >mysql_client_encoding</A > -- Returns the name of the character set</DT ><DT ><A HREF="function.mysql-close.html" >mysql_close</A > -- Close MySQL connection</DT ><DT ><A HREF="function.mysql-connect.html" >mysql_connect</A > -- Open a connection to a MySQL Server</DT ><DT ><A HREF="function.mysql-create-db.html" >mysql_create_db</A > -- Create a MySQL database</DT ><DT ><A HREF="function.mysql-data-seek.html" >mysql_data_seek</A > -- Move internal result pointer</DT ><DT ><A HREF="function.mysql-db-name.html" >mysql_db_name</A > -- Get result data</DT ><DT ><A HREF="function.mysql-db-query.html" >mysql_db_query</A > -- Send a MySQL query</DT ><DT ><A HREF="function.mysql-drop-db.html" >mysql_drop_db</A > -- Drop (delete) a MySQL database</DT ><DT ><A HREF="function.mysql-errno.html" >mysql_errno</A > -- Returns the numerical value of the error message from previous MySQL operation </DT ><DT ><A HREF="function.mysql-error.html" >mysql_error</A > -- Returns the text of the error message from previous MySQL operation </DT ><DT ><A HREF="function.mysql-escape-string.html" >mysql_escape_string</A > -- Escapes a string for use in a mysql_query. </DT ><DT ><A HREF="function.mysql-fetch-array.html" >mysql_fetch_array</A > -- Fetch a result row as an associative array, a numeric array, or both. </DT ><DT ><A HREF="function.mysql-fetch-assoc.html" >mysql_fetch_assoc</A > -- Fetch a result row as an associative array </DT ><DT ><A HREF="function.mysql-fetch-field.html" >mysql_fetch_field</A > -- Get column information from a result and return as an object </DT ><DT ><A HREF="function.mysql-fetch-lengths.html" >mysql_fetch_lengths</A > -- Get the length of each output in a result </DT ><DT ><A HREF="function.mysql-fetch-object.html" >mysql_fetch_object</A > -- Fetch a result row as an object</DT ><DT ><A HREF="function.mysql-fetch-row.html" >mysql_fetch_row</A > -- Get a result row as an enumerated array</DT ><DT ><A HREF="function.mysql-field-flags.html" >mysql_field_flags</A > -- Get the flags associated with the specified field in a result </DT ><DT ><A HREF="function.mysql-field-len.html" >mysql_field_len</A > -- Returns the length of the specified field </DT ><DT ><A HREF="function.mysql-field-name.html" >mysql_field_name</A > -- Get the name of the specified field in a result </DT ><DT ><A HREF="function.mysql-field-seek.html" >mysql_field_seek</A > -- Set result pointer to a specified field offset </DT ><DT ><A HREF="function.mysql-field-table.html" >mysql_field_table</A > -- Get name of the table the specified field is in </DT ><DT ><A HREF="function.mysql-field-type.html" >mysql_field_type</A > -- Get the type of the specified field in a result </DT ><DT ><A HREF="function.mysql-free-result.html" >mysql_free_result</A > -- Free result memory</DT ><DT ><A HREF="function.mysql-get-client-info.html" >mysql_get_client_info</A > -- Get MySQL client info</DT ><DT ><A HREF="function.mysql-get-host-info.html" >mysql_get_host_info</A > -- Get MySQL host info</DT ><DT ><A HREF="function.mysql-get-proto-info.html" >mysql_get_proto_info</A > -- Get MySQL protocol info</DT ><DT ><A HREF="function.mysql-get-server-info.html" >mysql_get_server_info</A > -- Get MySQL server info</DT ><DT ><A HREF="function.mysql-info.html" >mysql_info</A > -- Get information about the most recent query </DT ><DT ><A HREF="function.mysql-insert-id.html" >mysql_insert_id</A > -- Get the ID generated from the previous INSERT operation </DT ><DT ><A HREF="function.mysql-list-dbs.html" >mysql_list_dbs</A > -- List databases available on a MySQL server </DT ><DT ><A HREF="function.mysql-list-fields.html" >mysql_list_fields</A > -- List MySQL result fields</DT ><DT ><A HREF="function.mysql-list-processes.html" >mysql_list_processes</A > -- List MySQL processes</DT ><DT ><A HREF="function.mysql-list-tables.html" >mysql_list_tables</A > -- List tables in a MySQL database</DT ><DT ><A HREF="function.mysql-num-fields.html" >mysql_num_fields</A > -- Get number of fields in result</DT ><DT ><A HREF="function.mysql-num-rows.html" >mysql_num_rows</A > -- Get number of rows in result</DT ><DT ><A HREF="function.mysql-pconnect.html" >mysql_pconnect</A > -- Open a persistent connection to a MySQL server </DT ><DT ><A HREF="function.mysql-ping.html" >mysql_ping</A > -- Ping a server connection or reconnect if there is no connection</DT ><DT ><A HREF="function.mysql-query.html" >mysql_query</A > -- Send a MySQL query</DT ><DT ><A HREF="function.mysql-real-escape-string.html" >mysql_real_escape_string</A > -- Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection. </DT ><DT ><A HREF="function.mysql-result.html" >mysql_result</A > -- Get result data</DT ><DT ><A HREF="function.mysql-select-db.html" >mysql_select_db</A > -- Select a MySQL database</DT ><DT ><A HREF="function.mysql-stat.html" >mysql_stat</A > -- Get current system status</DT ><DT ><A HREF="function.mysql-tablename.html" >mysql_tablename</A > -- Get table name of field</DT ><DT ><A HREF="function.mysql-thread-id.html" >mysql_thread_id</A > -- Return the current thread ID</DT ><DT ><A HREF="function.mysql-unbuffered-query.html" >mysql_unbuffered_query</A > -- Send an SQL query to MySQL, without fetching and buffering the result rows </DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.msql.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.mysql-affected-rows.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >msql</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >mysql_affected_rows</TD ></TR ></TABLE ></DIV ></BODY ></HTML >