PHP functions to work with MySQL
72
1. mysql_affected_rows
mysql_affected_rows([link_id])
This function returns the number of affected rows in the previous MySQL INSERT, UPDATE, DELETE, or REPLACE operation performed with the specified link_id. If the link is not specified, then the last-opened link is assumed. It returns -1 if the previous operation failed.
2. mysql_close
mysql_close([link_id])
This function closes the current or specified (link_id) MySQL connection. If the link is a persistent link opened by mysql_pconnect (see below), this function call is ignored. As non-persistent connections are closed automatically by PHP at the end of a script, this function is usually not needed. This function returns true on success, false on failure.
3. mysql_connect
mysql_connect([hostname[:port|:/socket/path][, username[, password]]])
This function opens a connection to a MySQL server and returns a connection ID (which evaluates to true) that may be used in other MySQL-related functions. The following default values are assumed if they are not specified:
hostname:port 'localhost:3306'
username server process name
password ' '
If the connection attempt is unsuccessful, an error message will be displayed by default and the function will return false. To bypass display of the error message (e.g. to display your own by checking the return value), put '@' at the start of the function name (i.e.mysql_connect(...)).
4. mysql_create_db
mysql_create_db(db_name[, link_id])
This function creates a new MySQL database with the specified name, using the default or specified (link_id) MySQL connection. It returns true on success, or false on error. The function name mysql_createdb may also be used, but is deprecated. Note mysql_create_db is deprecated. Use mysql_query to issue a CREATE DATABASE command to MySQL instead.
5. mysql_data_seek
mysql_data_seek(result_id, row_number)
This function moves the internal result pointer of the result set identified by result_id to row number row_number, so that the next call to a mysql_fetch_* function will retrieve the specified row. It returns true on success, and false on failure. The first row in a result set is number 0.
6. mysql_db_name
mysql_db_name(result_id, row_number)
result_id should refer to a result set produced by a call to mysql_list_dbs (see below), and will
retrieve the name of the database listed on the row specified by row_number. The first row in a result set is row 0. The function name mysql_dbname may also be used, but is deprecated.
7. mysql_db_query
mysql_db_query(db_name, sql_query[, link_id])
This function selects the MySQL database identified by db_name as if with mysql_select_db, and then executes the specified MySQL query (sql_query). If the MySQL connection identifier (link_id) is not specified, PHP will use the currently active connection. If no such connection exists, PHP will attempt to open a connection by implicitly calling mysql_connect with default parameters.
8. mysql_drop_db
mysql_drop_db(db_name[, link_id])
This function drops (deletes) the specified database and all the tables it contains, using the default or specified (link_id) MySQL connection. It returns true on success or false on failure.
The function name mysql_dropdb may also be used, but is deprecated.
9.mysql_errno
mysql_errno([link_id])
This function returns the numerical value of the error message from the last MySQL operation on the default or specified (link_id) MySQL connection.
10. mysql_error
mysql_error([link_id])
This function returns the text of the error message from the last MySQL operation on the default or specified (link_id) MySQL connection.
11.mysql_escape_string
mysql_escape_string(string)
This function returns an escaped version of a string (with backslashes before special characters such
as quotes) for use in a MySQL query. This function is a little more thorough than addslashes or PHP's Magic Quotes feature, but those methods are generally sufficient (and in the case of Magic Quotes,automatic), so this function is rarely used.
12.mysql_fetch_assoc
mysql_fetch_assoc(result_id)
This function fetches a result row as an associative array. It's identical to mysql_fetch_array called with the MYSQL_ASSOC parameter.
13. mysql_fetch_lengths
mysql_fetch_lengths(result_id)
This function returns an array containing the lengths of each of the fields in the last-fetched row of the specified result set.
14.mysql_fetch_object
mysql_fetch_object(result_id)
This function returns the next result row from result_id in the form of an object, and advances the internal row pointer of the result set to the next row. Column values for the row become accessible as named properties of the object (e.g. $row->user for the value of the user field in the $row object).
15. mysql_fetch_row
mysql_fetch_row(result_id)
This function fetches a result row as numerical array. Identical to mysql_fetch_array called with the MYSQL_NUM parameter.
16. mysql_field_flags
mysql_field_flags(result_id, field_position)
This function returns a string containing the flags associated with the specified field (field_position) in the specified result set (result_id). The flags are separated by spaces in the returned string. Possible flags are: not_null, primary_key, unique_key, multiple_key, blob, unsigned, zerofill, binary, enum, auto_increment, and timestamp. The function name mysql_fieldflags may also be used, but is deprecated.
17.mysql_field_len
mysql_field_len(result_id, field_position)
This function returns the length of the specified field (field_position) in a result set (result_id).
The function name mysql_fieldlen may also be used, but is deprecated.
18.mysql_field_name
mysql_field_name(result_id, field_position)
This function returns the name of the specified field (field_position) in a result set (result_id).
The function name mysql_fieldname may also be used, but is deprecated.
19.mysql_get_client_info
mysql_get_client_info()
This function returns a string indicating the version of the MySQL client library that PHP is using (e.g.
'3.23.54a').
20. mysql_get_host_info
mysql_get_host_info([link_id])
This function returns a string describing the type of connection and server host name for the specified (link_id) or last opened MySQL connection (e.g. 'Localhost via UNIX socket').
21. mysql_list_dbs
mysql_list_dbs([link_id])
This function returns a result set containing a list of the databases available from the current or specified (link_id) MySQL connection. Use mysql_db_name to retrieve the individual database names from this result set. The function name mysql_listdbs may also be used, but is deprecated.
22. mysql_list_tables
mysql_list_tables(db_name[, link_id ])
This function returns a result set containing a list of the tables in the specified database (db_name) from the current or specified (link_id) MySQL connection. Use mysql_tablename to retrieve the individual table names from this result set. The function name mysql_listtables may also be used, but is deprecated.
23.mysql_pconnect
mysql_pconnect([hostname[:port|:/socket/path][, username[, password]]])
This function opens a persistent connection to a MySQL Server. Works the same as mysql_connect, except that the connection is not closed by mysql_close or at the end of the script. If a persistent connection is already found to exist with the specified parameters, then this is used, avoiding the creation of a new one.
24. mysql_query
mysql_query(sql_query[, link_id])
This function executes the specified MySQL query (sql_query) on the currently selected database. If the MySQL connection identifier (link_id) is not specified, PHP will use the currently active connection. If no such connection exists, PHP will attempt to open a connection by implicitly calling mysql_connect with default parameters.
25. mysql_result
mysql_result(result_id, row[, field])
This function returns the value of a particular field of the specified row (row) of the specified result set (result_id). The field argument may be the name of the field (either fieldname or
dbname.fieldname), or its numerical position, where the first field in a row is at position 0. If field is not specified, then 0 is assumed.
26.mysql_select_db
mysql_select_db(db_name[, link_id])
This function selects the default database (db_name) for the current or specified (link_id) MySQL connection. The function name mysql_selectdb may also be used, but is deprecated.
27. mysql_tablename
mysql_tablename(result_id, row_number)
result_id should refer to a result set produced by a call to mysql_list_tables, and will retrieve the name of the table listed on the row specified by row_number. The first row in a result set is row 0.
I haven't listed all of them, just the commonly used and important ones. If you need any help regarding any of these or the one's not listed here, just let me know.
PrintShare it! — Rate it: up down flag this hub
Comments
yup....i totally agree
Excellent hub!!
Thanks.
very nice.
do you write programs for web sites ?
you should try flex with amfphp to free yourself from jscript and build beautiful user interfaces.
cheers.
Thanks for the suggestion zampano.













Erick Smart says:
4 months ago
Excellent list of functions. PHP is such a easy to use language and the MySQL platform is very intuitive, making it an ideal setup for beginning programmers.