AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP connect to MySQL


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP connect to MySQL

PHP has three different ways to connect and interact with the MySQL database:

Which methods are more advantageous to MySQLi or PDO?
The difference between MySQLi and PDO

Each, MySQLi and PDO have their advantages:

The PDO will work on many more  database systems, will work on 12 different database systems, while MySQLi will only work with MySQL databases.

Both are object-oriented, but MySQLi also offers a procedural API.

Both PDO and MySQLi support Prepared Statements witch protect against SQL injection and are very important for web application security.

In this chapter as well as in the following chapters we will show you three methods of working with PHP and MySQL:

 

MySQL connect using MySQLi Object-Oriented

Before accessing data from the MySQL database, we must be able to connect to the server:

Example (this is HTML - PHP editor, change text on this window)

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user, root, your_password, database name) */
$link = new mysqli("localhost", "root", "your_password", "DB_name");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . $link->connect_error);
}

// Print host information
echo "Connect Successfully. Host info: " . $link->host_info;<br />

// your code here

// Close the Connection to DataBase
$link->close();
?>

 

Related subjects:
Delete file in PHP GET / POST store data PHP simple login

Tags: php connect to mysql: database, pdo, xampp, ssl, using pdo, and run query, docker, and display table, workbench database, class
php mysql connect: to database, error, port, function, timeout, db, test, string, pdo, object oriented

 

MySQL connect using MySQLi Procedural.

Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password", "DB_name")

Example (this is HTML - PHP editor, change text on this window)

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting ("localhost", "root", "your_password", "DB_name") */
$link = mysqli_connect("localhost", "root", "your_password", "DB_name");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Print host information
echo "Connected successfully";

// your code here

// Close the Connection to DataBase
mysqli_close($link);
?>

 

MySQL connect using PDO extension.

PDO extension. Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")

Example (this is HTML - PHP editor, change text on this window)

<?php
/* /* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting ("localhost", "root", "your_password")
*/
try{

$pdo = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Print host information
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

// your code here

// Close the Connection
$pdo = null;
?>

 



php connect to mysql: database, pdo, xampp, ssl, using pdo, and run query, docker, and display table, workbench database, class
php mysql connect: to database, error, port, function, timeout, db, test, string, pdo, object oriented
PHP connect to MySQLi - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...