Connect PHP to MySQL
You can connect PHP to MySQL in two ways. You can use either MySQLi or PDO.
First Way, If you use MySQLi then you can write syntax in both object-oriented programming and procedural programming.
MySQLi Object-Oriented Style
//open connection and select the database.
$connection = new mysqli($host, $user, $password, $databaseName);
//close connection.
$connection->close();
$connection = new mysqli($host, $user, $password, $databaseName);
//close connection.
$connection->close();
MySQLi Procedural Style
//open connection and select the database.
$connection = mysqli_connect($host, $user, $password, $databaseName);
//you can also change database.
mysqli_select_db($connection, $otherDatabaseName);
//close connection.
mysqli_close($connection);
$connection = mysqli_connect($host, $user, $password, $databaseName);
//you can also change database.
mysqli_select_db($connection, $otherDatabaseName);
//close connection.
mysqli_close($connection);
Second Way, If you use PDO.
//open connection.
$connection = new PDO("mysql:host=$host;dbname=$database", $user, $password);
//close connection.
$connection = NULL;
$connection = new PDO("mysql:host=$host;dbname=$database", $user, $password);
//close connection.
$connection = NULL;
Good JOb ! plz expand your posts to php frameworks(specially Yii)
ReplyDeleteSure i will. Stay tuned.
ReplyDelete