MySQL functions
MySQL
These functions allow you to access MySQL database servers.
More information about MySQL can be found at http://www.mysql.com/.
mysql_affected_rows
Get number of affected rows in previous MySQL
operation
Description
int mysql_affected_rows
int link_identifier
mysql_affected_rows returns the number of
rows affected by the last INSERT, UPDATE or DELETE query on the
server associated with the specified link identifier. If the
link identifier isn't specified, the last opened link is assumed.
If the last query was a DELETE query with no WHERE clause, all
of the records will have been deleted from the table but this
function will return zero.
This command is not effective for SELECT statements, only on
statements which modify records. To retrieve the number of rows
returned from a SELECT, use mysql_num_rows.
mysql_change_user
Change logged in user on active connection
Description
int mysql_change_user
string user
string password
string database
int link_identifier
mysql_change_user changes the logged in user on the current
active connection, or, if specified on the connection given by the link identifier.
If a database is specified, this will default or current database after the
user has been changed. If the new user/password combination fails to be authorized
the current connected user stays active.
Note that this function was introduced in PHP 3.0.13 and requires MySQL
3.23.3 or higher.
mysql_close
close MySQL connection
Description
int mysql_close
int link_identifier
Returns: true on success, false on error
mysql_close closes the link to a MySQL
database that's associated with the specified link identifier.
If the link identifier isn't specified, the last opened link is
assumed.
Note that this isn't usually necessary, as non-persistent open
links are automatically closed at the end of the script's
execution.
mysql_close will not close persistent links
generated by mysql_pconnect.
See also: mysql_connect, and
mysql_pconnect.
mysql_connect
Open a connection to a MySQL Server
Description
int mysql_connect
string
hostname
:port
:/path/to/socket
string username
string password
Returns: A positive MySQL link identifier on success, or false on
error.
mysql_connect establishes a connection to a
MySQL server. All of the arguments are optional, and if they're
missing, defaults are assumed ('localhost', user name of the user
that owns the server process, empty password).
The hostname string can also include a port
number. eg. "hostname:port" or a path to a socket
eg. ":/path/to/socket" for the localhost.
Support for ":port" wass added in 3.0B4.
Support for the ":/path/to/socket" was added in
3.0.10.
In case a second call is made to
mysql_connect with the same arguments, no
new link will be established, but instead, the link identifier of
the already opened link will be returned.
The link to the server will be closed as soon as the execution of
the script ends, unless it's closed earlier by explicitly calling
mysql_close.
See also
mysql_pconnect, and
mysql_close.
mysql_create_db
Create a MySQL database
Description
int mysql_create_db
string database name
int link_identifier
mysql_create_db attempts to create a new
database on the server associated with the specified link
identifier.
See also: mysql_drop_db. For downwards
compatibility mysql_createdb can also be
used.
mysql_data_seek
Move internal result pointer
Description
int mysql_data_seek
int result_identifier
int row_number
Returns: true on success, false on failure
mysql_data_seek moves the internal row
pointer of the MySQL result associated with the specified result
identifier to point to the specified row number. The next call
to mysql_fetch_row would return that row.
Row_number starts at 0.
mysql_db_query
Send an MySQL query to MySQL
Description
int mysql_db_query
string database
string query
int link_identifier
Returns: A positive MySQL result identifier to the query result,
or false on error.
mysql_db_query selects a database and
executes a query on it. If the optional link identifier isn't
specified, the function will try to find an open link to the
MySQL server and if no such link is found it'll try to create one
as if mysql_connect was called with no
arguments
See also mysql_connect. For downwards
compatibility mysql can also be used.
mysql_drop_db
Drop (delete) a MySQL database
Description
int mysql_drop_db
string database_name
int link_identifier
Returns: true on success, false on failure.
mysql_drop_db attempts to drop (remove) an
entire database from the server associated with the specified
link identifier.
See also: mysql_create_db. For downward
compatibility mysql_dropdb can also be used.
mysql_errno
Returns the number of the error message from previous
MySQL operation
Description
int mysql_errno
int link_identifier
Errors coming back from the mySQL database backend no longer
issue warnings. Instead, use these functions to retrieve the
error number.
<?php
mysql_connect("marliesle");
echo mysql_errno().": ".mysql_error()."<BR>";
mysql_select_db("nonexistentdb");
echo mysql_errno().": ".mysql_error()."<BR>";
$conn = mysql_query("SELECT * FROM nonexistenttable");
echo mysql_errno().": ".mysql_error()."<BR>";
?>
See also: mysql_error
mysql_error
Returns the text of the error message from previous
MySQL operation
Description
string mysql_error
int link_identifier
Errors coming back from the mySQL database backend no longer
issue warnings. Instead, use these functions to retrieve the
error string.
<?php
mysql_connect("marliesle");
echo mysql_errno().": ".mysql_error()."<BR>";
mysql_select_db("nonexistentdb");
echo mysql_errno().": ".mysql_error()."<BR>";
$conn = mysql_query("SELECT * FROM nonexistenttable");
echo mysql_errno().": ".mysql_error()."<BR>";
?>
See also: mysql_errno
mysql_fetch_array
Fetch a result row as an associative array
Description
array mysql_fetch_array
int result
int
result_type
Returns an array that corresponds to the fetched row, or false
if there are no more rows.
mysql_fetch_array is an extended version of
mysql_fetch_row. In addition to storing the
data in the numeric indices of the result array, it also stores
the data in associative indices, using the field names as keys.
If two or more columns of the result have the same field names,
the last column will take precedence. To access the other column(s)
of the same name, you must the numeric index of the column or
make an alias for the column.
select t1.f1 as foo t2.f1 as bar from t1, t2
An important thing to note is that using
mysql_fetch_array is NOT significantly
slower than using mysql_fetch_row, while it
provides a significant added value.
The optional second argument result_type in
mysql_fetch_array is a constant and can take
the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.
For further details, also see
mysql_fetch_row
mysql fetch array
<?php
mysql_connect($host,$user,$password);
$result = mysql_db_query("database","select * from table");
while($row = mysql_fetch_array($result)) {
echo $row["user_id"];
echo $row["fullname"];
}
mysql_free_result($result);
?>
mysql_fetch_field
Get column information from a result and return as an
object
Description
object mysql_fetch_field
int result
int field_offset
Returns an object containing field information.
mysql_fetch_field can be used in order to
obtain information about fields in a certain query result. If
the field offset isn't specified, the next field that wasn't yet
retrieved by mysql_fetch_field is retrieved.
The properties of the object are:
name - column name
table - name of the table the column belongs to
max_length - maximum length of the column
not_null - 1 if the column cannot be null
primary_key - 1 if the column is a primary key
unique_key - 1 if the column is a unique key
multiple_key - 1 if the column is a non-unique key
numeric - 1 if the column is numeric
blob - 1 if the column is a BLOB
type - the type of the column
unsigned - 1 if the column is unsigned
zerofill - 1 if the column is zero-filled
See also mysql_field_seek
mysql_fetch_lengths
Get the length of each output in a result
Description
array mysql_fetch_lengths
int result
Returns: An array that corresponds to the lengths of each field
in the last row fetched by mysql_fetch_row,
or false on error.
mysql_fetch_lengths stores the lengths of
each result column in the last row returned by
mysql_fetch_row,
mysql_fetch_array, and
mysql_fetch_object in an array, starting at
offset 0.
See also: mysql_fetch_row.
mysql_fetch_object
Fetch a result row as an object
Description
object mysql_fetch_object
int result
int result_typ
Returns an object with properties that correspond to the fetched
row, or false if there are no more rows.
mysql_fetch_object is similar to
mysql_fetch_array, with one difference - an
object is returned, instead of an array. Indirectly, that means
that you can only access the data by the field names, and not by
their offsets (numbers are illegal property names).
The optional argument result_typ is a
constant and can take the following values: MYSQL_ASSOC,
MYSQL_NUM, and MYSQL_BOTH.
Speed-wise, the function is identical to
mysql_fetch_array, and almost as quick as
mysql_fetch_row (the difference is
insignificant).
mysql fetch object
<?php
mysql_connect($host,$user,$password);
$result = mysql_db_query("database","select * from table");
while($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}
mysql_free_result($result);
?>
See also: mysql_fetch_array and
mysql_fetch_row.
mysql_fetch_row
Get a result row as an enumerated array
Description
array mysql_fetch_row
int result
Returns: An array that corresponds to the fetched row, or false
if there are no more rows.
mysql_fetch_row fetches one row of data from
the result associated with the specified result identifier. The
row is returned as an array. Each result column is stored in an
array offset, starting at offset 0.
Subsequent call to mysql_fetch_row would
return the next row in the result set, or false if there are no
more rows.
See also: mysql_fetch_array,
mysql_fetch_object,
mysql_data_seek,
mysql_fetch_lengths, and
mysql_result.
mysql_field_name
Get the name of the specified field in a
result
Description
string mysql_field_name
int result
int field_index
mysql_field_name returns the name of the
specified field. Arguments to the function is the result
identifier and the field index, ie.
mysql_field_name($result,2);
Will return the name of the second field in the result associated
with the result identifier.
For downwards compatibility mysql_fieldname
can also be used.
mysql_field_seek
Set result pointer to a specified field offset
Description
int mysql_field_seek
int result
int field_offset
Seeks to the specified field offset. If the next call to
mysql_fetch_field won't include a field
offset, this field would be returned.
See also: mysql_fetch_field.
mysql_field_table
Get name of the table the specified field is
in
Description
string mysql_field_table
int result
int field_offset
Get the table name for field. For downward compatibility
mysql_fieldtable can also be used.
mysql_field_type
Get the type of the specified field in a
result
Description
string mysql_field_type
int result
int field_offset
mysql_field_type is similar to the
mysql_field_name function. The arguments are
identical, but the field type is returned. This will be one of
"int", "real", "string", "blob", or others as detailed in the
MySQL documentation.
mysql field types
<?php
mysql_connect("localhost:3306");
mysql_select_db("wisconsin");
$result = mysql_query("SELECT * FROM onek");
$fields = mysql_num_fields($result);
$rows = mysql_num_rows($result);
$i = 0;
$table = mysql_field_table($result, $i);
echo "Your '".$table."' table has ".$fields." fields and ".$rows." records <BR>";
echo "The table has the following fields <BR>";
while ($i < $fields) {
$type = mysql_field_type ($result, $i);
$name = mysql_field_name ($result, $i);
$len = mysql_field_len ($result, $i);
$flags = mysql_field_flags ($result, $i);
echo $type." ".$name." ".$len." ".$flags."<BR>";
$i++;
}
mysql_close();
?>
For downward compatibility mysql_fieldtype
can also be used.
mysql_field_flags
Get the flags associated with the specified field in a
result
Description
string mysql_field_flags
int result
int field_offset
mysql_field_flags returns the field flags of
the specified field. The flags are reported as a single word
per flag separated by a single space, so that you can split the
returned value using explode.
The following flags are reported, if your version of MySQL
is current enough to support them: "not_null", "primary_key",
"unique_key", "multiple_key", "blob", "unsigned", "zerofill",
"binary", "enum", "auto_increment", "timestamp".
For downward compatibility mysql_fieldflags
can also be used.
mysql_field_len
Returns the length of the specified field
Description
int mysql_field_len
int result
int field_offset
mysql_field_len returns the length of the
specified field. For downward compatibility
mysql_fieldlen can also be used.
mysql_free_result
Free result memory
Description
int mysql_free_result
int result
mysql_free_result only needs to be called if
you are worried about using too much memory while your script is
running. All associated result memory for the specified result
identifier will automatically be freed.
For downward compatibility mysql_freeresult
can also be used.
mysql_insert_id
Get the id generated from the previous INSERT
operation
Description
int mysql_insert_id
int link_identifier
mysql_insert_id returns the ID generated for
an AUTO_INCREMENTED field. It will return the auto-generated ID
returned by the last INSERT query performed using the given
link_identifier. If
link_identifier isn't specified, the last
opened link is assumed.
mysql_list_fields
List MySQL result fields
Description
int mysql_list_fields
string database_name
string table_name
int link_identifier
mysql_list_fields retrieves information
about the given tablename. Arguments are the database name and
the table name. A result pointer is returned which can be used
with
mysql_field_flags,
mysql_field_len,
mysql_field_name, and
mysql_field_type.
A result identifier is a positive integer. The function returns
-1 if a error occurs. A string describing the error will be
placed in $phperrmsg, and unless the function
was called as @mysql() then this error string
will also be printed out.
For downward compatibility mysql_listfields
can also be used.
mysql_list_dbs
List databases available on on MySQL server
Description
int mysql_list_dbs
int link_identifier
mysql_list_dbs will return a result pointer
containing the databases available from the current mysql
daemon. Use the mysql_tablename function to
traverse this result pointer.
For downward compatibility mysql_listdbs can
also be used.
mysql_list_tables
List tables in a MySQL database
Description
int mysql_list_tables
string database
int
link_identifier
mysql_list_tables takes a database name and
returns a result pointer much like the
mysql_db_query function. The
mysql_tablename function should be used to
extract the actual table names from the result pointer.
For downward compatibility mysql_listtables
can also be used.
mysql_num_fields
Get number of fields in result
Description
int mysql_num_fields
int result
mysql_num_fields returns the number of
fields in a result set.
See also:
mysql_db_query,
mysql_query,
mysql_fetch_field,
mysql_num_rows.
For downward compatibility mysql_numfields
can also be used.
mysql_num_rows
Get number of rows in result
Description
int mysql_num_rows
int result
mysql_num_rows returns the number of rows in
a result set.
See also:
mysql_db_query,
mysql_query and,
mysql_fetch_row.
For downward compatibility mysql_numrows can
also be used.
mysql_pconnect
Open a persistent connection to a MySQL Server
Description
int mysql_pconnect
string
hostname
:port
:/path/to/socket
string
username
string
password
Returns: A positive MySQL persistent link identifier on success,
or false on error
mysql_pconnect establishes a connection to a
MySQL server. All of the arguments are optional, and if they're
missing, defaults are assumed ('localhost', user name of the user
that owns the server process, empty password).
The hostname string can also include a port
number. eg. "hostname:port" or a path to a socket
eg. ":/path/to/socket" for the localhost.
Support for ":port" wass added in 3.0B4.
Support for the ":/path/to/socket" was added in
3.0.10.
mysql_pconnect acts very much like
mysql_connect with two major differences.
First, when connecting, the function would first try to find a
(persistent) link that's already open with the same host,
username and password. If one is found, an identifier for it
will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when
the execution of the script ends. Instead, the link will remain
open for future use (mysql_close will not
close links established by mysql_pconnect).
This type of links is therefore called 'persistent'.
mysql_query
Send an SQL query to MySQL
Description
int mysql_query
string query
int
link_identifier
mysql_query sends a query to the currently
active database on the server that's associated with the
specified link identifier. If
link_identifier isn't specified, the last
opened link is assumed. If no link is open, the function tries
to establish a link as if mysql_connect was
called with no arguments, and use it.
The query string should not end with a semicolon.
mysql_query returns TRUE (non-zero) or FALSE
to indicate whether or not the query succeeded. A return value
of TRUE means that the query was legal and could be executed by
the server. It does not indicate anything about the number of
rows affected or returned. It is perfectly possible for a query
to succeed but affect no rows or return no rows.
The following query is syntactically invalid, so
mysql_query fails and returns FALSE:
mysql_query
<?php
$result = mysql_query ("SELECT * WHERE 1=1")
or die ("Invalid query");
?>
The following query is semantically invalid if
my_col is not a column in the table
my_tbl, so mysql_query
fails and returns FALSE:
mysql_query
<?php
$result = mysql_query ("SELECT my_col FROM my_tbl")
or die ("Invalid query");
?>
mysql_query will also fail and return FALSE
if you don't have permission to access the table(s) referenced by
the query.
Assuming the query succeeds, you can call
mysql_affected_rows to find out how many
rows were affected (for DELETE, INSERT, REPLACE, or UPDATE
statements). For SELECT statements,
mysql_query returns a new result identifier
that you can pass to mysql_result. When you
are done with the result set, you can free the resources
associated with it by calling
mysql_free_result.
See also: mysql_affected_rows,
mysql_db_query,
mysql_free_result,
mysql_result,
mysql_select_db, and
mysql_connect.
mysql_result
Get result data
Description
int mysql_result
int result
int row
mixed
field
mysql_result returns the contents of one
cell from a MySQL result set. The field argument can be the
field's offset, or the field's name, or the field's table dot
field's name (fieldname.tablename). If the column name has been
aliased ('select foo as bar from...'), use the alias instead of
the column name.
When working on large result sets, you should consider using one
of the functions that fetch an entire row (specified below). As
these functions return the contents of multiple cells in one
function call, they're MUCH quicker than
mysql_result. Also, note that specifying a
numeric offset for the field argument is much quicker than
specifying a fieldname or tablename.fieldname argument.
Calls mysql_result should not be mixed
with calls to other functions that deal with the result set.
Recommended high-performance alternatives:
mysql_fetch_row,
mysql_fetch_array, and
mysql_fetch_object.
mysql_select_db
Select a MySQL database
Description
int mysql_select_db
string database_name
int link_identifier
Returns: true on success, false on error
mysql_select_db sets the current active
database on the server that's associated with the specified link
identifier. If no link identifier is specified, the last opened
link is assumed. If no link is open, the function will try to
establish a link as if mysql_connect was
called, and use it.
Every subsequent call to mysql_query will be
made on the active database.
See also:
mysql_connect,
mysql_pconnect, and
mysql_query
For downward compatibility mysql_selectdb
can also be used.
mysql_tablename
Get table name of field
Description
string mysql_tablename
int result
int i
mysql_tablename takes a result pointer
returned by the mysql_list_tables function
as well as an integer index and returns the name of a table. The
mysql_num_rows function may be used to
determine the number of tables in the result pointer.
mysql_tablename example
<?php
mysql_connect ("localhost:3306");
$result = mysql_list_tables ("wisconsin");
$i = 0;
while ($i < mysql_num_rows ($result)) {
$tb_names[$i] = mysql_tablename ($result, $i);
echo $tb_names[$i] . "<BR>";
$i++;
}
?>