Database connection with PHP and include PHP files in another page
Yes, guys, in the second part of our PHP lesson, I will show you database connection and including a php file in another php file.
We will use the PDO connection for the connection to our database.
PDO is short for PHP Data Object.
It has been used since PHP 5.1.
The most important features of PDO are both simple database connection and coding system, and you can connect to many different databases with the same codes, except mysql, without making any code changes.
a- We connect to our database and close our connection at the end of the page.
You can connect Mysql DB with PDO with the code below.
As additional information, you can provide multiple database connections by changing the name of "connect_db" (you can give any name here) and duplicating these codes.
b- Generally, include is used for db connection.
We use the "include" command to add a php page to another php page.
The reason for this is to do this quickly with a single page instead of constantly adding the same codes to our multiple php files.
In addition, when we need to make a change in our php file, it will be sufficient to edit our included file only.
For example, we saved the above db connection codes by creating a db.php file and adding it to it.
We saved the db connection closing codes by creating a file called db_closed.php.
Finally, we included these files on the top and bottom lines of our index.php main file.
Yes, guys, in the second part of our PHP lesson, I will show you database connection and including a php file in another php file.
We will use the PDO connection for the connection to our database.
PDO is short for PHP Data Object.
It has been used since PHP 5.1.
The most important features of PDO are both simple database connection and coding system, and you can connect to many different databases with the same codes, except mysql, without making any code changes.
a- We connect to our database and close our connection at the end of the page.
You can connect Mysql DB with PDO with the code below.
PHP:
try
{
$connect_db= new PDO('mysql:host='.sql_server.';port=3306;dbname='.$sql_db.';charset=utf8;',$sql_user,$sql_pass, array(PDO::ATTR_PERSISTENT => true)) or die('Could not connect: ');
$connect_db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect_db->setAttribute(PDO::ATTR_PERSISTENT, PDO::NULL_TO_STRING );
$connect_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connect_db->exec("SET NAMES UTF8");
}
catch(PDOException $e)
{
echo 'Db Connection Error'
}
/*
We write all our php codes in this range
*/
// to close the DB when you're done
$connect_db = null;
As additional information, you can provide multiple database connections by changing the name of "connect_db" (you can give any name here) and duplicating these codes.
b- Generally, include is used for db connection.
We use the "include" command to add a php page to another php page.
The reason for this is to do this quickly with a single page instead of constantly adding the same codes to our multiple php files.
In addition, when we need to make a change in our php file, it will be sufficient to edit our included file only.
For example, we saved the above db connection codes by creating a db.php file and adding it to it.
We saved the db connection closing codes by creating a file called db_closed.php.
Finally, we included these files on the top and bottom lines of our index.php main file.
PHP:
<?PHP
// db.php file
$sql_server = 'localhost'; // Usually localhost or IP number.
$sql_db = 'db_name'; // Db name
$sql_user = 'db_username'; // Database username
$sql_pass= 'db_password'; // Database password
try
{
$connect_db= new PDO('mysql:host='.$sql_server.';port=3306;dbname='.$sql_db.';charset=utf8;',$sql_user,$sql_pass, array(PDO::ATTR_PERSISTENT => true)) or die('Could not connect: ');
$connect_db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect_db->setAttribute(PDO::ATTR_PERSISTENT, PDO::NULL_TO_STRING );
$connect_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connect_db->exec("SET NAMES UTF8");
}
catch(PDOException $e)
{
echo 'Db Connection Error'
}
?>
PHP:
<?PHP
// db_closed.php file
$connect_db = null;
?>
PHP:
<?PHP
/* We include the index.php file and our database files in our index.php file. */
echo 'We made a database connection';
// Our DB closing codes are in this php file
include 'db_closed.php'
?>