This is a discussion on DB Connection in PHP within the PHP Programming forums, part of the Web Development category; This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database. PHP ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database. PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| Sponsored Links |
| |||
| mysql_change_user() changes the logged in user of the current active connection, or the connection given by the optional link_identifier parameter. If a database is specified, this will be the current database after the user has been changed. If the new user and password authorization fails, the current connected user stays active.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_client_encoding() retrieves the character_set variable from MySQL. Example: PHP Code: Code: The current character set is: latin1
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_real_escape_string() escapes special characters in the unescaped_string, taking into account the current character set of the connection so that it is safe to place it in a mysql_query(). If binary data is to be inserted, this function must be used. mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a. This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Simple mysql_real_escape_string() example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Using mysql_real_escape_string() around each variable prevents SQL Injection. This example demonstrates the "best practice" method for querying a database, independent of the Magic Quotes setting. PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| An example SQL Injection Attack PHP Code: Code: SELECT * FROM users WHERE name='aidan' AND password='' OR ''=''
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. If link_identifier isn't defined, the last MySQL connection is used.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| If magic_quotes_gpc is enabled, first apply stripslashes() to the data. Using this function on data which has already been escaped will escape the data twice.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_real_escape_string() does not escape % and _. These are wildcards in MySQL if combined with LIKE, GRANT, or REVOKE.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_data_seek() example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_escape_string () will escape the unescaped_string, so that it is safe to place it in a mysql_query(). This function is deprecated. This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current charset setting.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_escape_string() example PHP Code: Code: Escaped string: Zak\'s Laptop
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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(). Example: PHP Code: Code: not_null primary_key auto_increment
Array
(
[0] => not_null
[1] => primary_key
[2] => auto_increment
)
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_field_len() returns the length of the specified field. Example: PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| mysql_field_name() returns the name of the specified field index. Example: PHP Code: Code: user_id password
__________________ With, J. Jeyaseelan Everything Possible |