There are people who think that if people like their videos they'll subscribe, so they shouldn't bother people by asking. This is, simply put, wrong. When people are browsing YouTube their minds are on what video they want to watch next, not whether or not they want to
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
46 views14 pages
Unit2 (Second)
There are people who think that if people like their videos they'll subscribe, so they shouldn't bother people by asking. This is, simply put, wrong. When people are browsing YouTube their minds are on what video they want to watch next, not whether or not they want to
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14
PHP Connection with MySql
PHP Connect to MySQL
PHP 5 and later can work with a MySQL
database using: MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects) Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012. Should I Use MySQLi or PDO?
If you need a short answer, it would be "Whatever you like".
Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included. Both are object-oriented, but MySQLi also offers a procedural API. Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security. There are three ways of working with MySQL and PHP • MySQLi (object-oriented) • MySQLi (procedural) • PDO Using MySQLi object-oriented procedure:
We can use the MySQLi
object-oriented procedure to establish a connection to MySQL database from a PHP script. Example <?php $servername = "localhost"; $username = "username"; $password = "password"; // Creating connection $conn = new mysqli($servername, $username, $password); // Checking connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> Explanation: We can create an instance of the mysqli class providing all the necessary details required to establish the connection such as host, username, password etc. If the instance is created successfully then the connection is successful otherwise there is some error in establishing connection. Using MySQLi procedural procedure
There is also a procedural approach of MySQLi
to establish a connection to MySQL database from a PHP script as described below. • <?php $servername = "localhost"; $username = "username"; $password = "password"; • // Creating connection • $conn = mysqli_connect($servername, $username, $password); • // Checking connection • if (!$conn) • { • die("Connection failed: " . mysqli_connect_error()); } • echo "Connected successfully"; ?> Explanation • In MySQLi procedural approach instead of creating an instance we can use the mysqli_connect() function available in PHP to establish a connection. This function takes the information as arguments such as host, username , password , database name etc. This function returns MySQL link identifier on successful connecction or FALSE when failed to establish a connection. Using PDO procedure: • PDO stands for PHP Data Objects. That is, in this method we connect to the database using data objects in PHP as described below: <?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // setting the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> Explanation: The exception class in PDO is used to handle any problems that may occur in our database queries. If an exception is thrown within the try{ } block, the script stops executing and flows directly to the first catch(){ } block Closing A Connection
When we establish a connection to MySQL database from a
PHP script , we should also disconnect or close the connection when our work is finished. Here we have described the syntax of closing the connection to a MySQL database in all 3 methods described above. We have assumed that the reference to the connection is stored in $conn variable. – Using MySQLi object oriented procedure Syntax$conn->close(); – Using MySQLi procedural procedure Syntaxmysqli_close($conn); – Using PDO procedure Syntax$conn = null;