Skip to main content

PHP & MySQL

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();

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);

Second Way, If you use PDO.
//open connection.
$connection = new PDO("mysql:host=$host;dbname=$database", $user, $password);

//close connection.
$connection = NULL;

Comments

Post a Comment

Popular posts from this blog

HTTP Server : Apache

Apache is a open source HTTP web server. It handles HTTP Requests sent to it and then it is able to them. Apache is comprised of Two main building Blocks with the Latter being comprised of many other little building blocks. The Building Blocks are the Apache Core and then the Apache Modules that in a sense extend the Apache core. What is a Web Server?  A Web Server is any Machine that receives requests from a client machine and is able to turn around process the requests and send back a response. This is usually in terms of a Web Server to send back a Web pages when people what to go navigate to a webpage that is hosted on that server now one may ask how do you send and receive the information to and from the server. This is where HTTP begins to play a role into how this all goes about. What is HTTP? HTTP Protocol: HTTP stands for H yper- T ext- T ransfer- P rotocol This is the protocol that is used in order to send and receive information from the server. This is...

Web technology

The open nature of the World Wide Web presents incredible opportunities for people who want to create websites or online applications. To take full advantage of the web's capabilities, you need to know how to use them. Web technologies   Basics HTML HyperText Markup Language (HTML) is used to describe and define the content of a webpage. CSS Cascading Style Sheets (CSS) are used to describe the appearance or presentation of content on a webpage. HTTP Hypertext Transfer Protocol (HTTP) is used to deliver HTML and other hypermedia documents on the web. Scripting JavaScript JavaScript is the programming language that runs in your browser. You can use it to add interactivity and other dynamic features to your website or application.