PHP / MYSQL CONNECTION

PHP has three standard APIs for database connections:
MYSQL
MYSQLi – MySQL Improved
PDO – PHP Data Objects (Best)

The traditional mysql API certainly gets the job done, and has become so popular largely due to the fact that it makes the process of retrieving some records from a database as easy as possible.

A typical Example is shown below

# Connection to a specific database
[php]
mysql_connect(‘host_machine’, ‘your_username’, ‘your_password’) or die(‘Connection failed: ‘ . mysql_error());
[/php]

# Selecting a database
[php]
mysql_select_db(‘Your_database’) or die(‘Database connection failed’);
[/php]

# Retrieve records from the database
[php] $sql = "SELECT username, password FROM table_name";
$statement = mysql_query($query) ;
[/php]

# Display the result

[php]
while ($rs = mysql_fetch_array($statement)) {
echo $rs[‘username’];
}
[/php]

Simple, but has some significant downsides
1) It is officially deprecated.
2) Difficult to sanitize input because the developer will have to do it himself
3) Code above can only work with MYSQL the API does not support other database

MySQL

Leave a Reply

Your email address will not be published. Required fields are marked *