MySql Connection
<?php
$servername = "localhost";
$username = "root";
$password = "data base password";
// Create database connection$con = new mysqli($servername, $username, $password);
// Check database connectionif ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
?>
create a database db-restapi
----
And create a table
$servername = "localhost";
$username = "root";
$password = "data base password";
// Create database connection$con = new mysqli($servername, $username, $password);
// Check database connectionif ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
?>
create a database db-restapi
----
And create a table
CREATE TABLE IF NOT EXISTS `categories` ( `id` int (11) NOT NULL AUTO_INCREMENT, ` name ` varchar (256) NOT NULL , `description` text NOT NULL , `created` datetime NOT NULL , `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
Connect to database using PDO
----------------- <?php class Database{ // use your own database and username and pasword private $host = "localhost" ; private $db_name = "any_db" ; private $username = "root" ; private $password = "" ; public $conn ; // get the database connection public function getConnection(){ $this ->conn = null; try { $this ->conn = new PDO( "mysql:host=" . $this ->host . ";dbname=" . $this ->db_name, $this ->username, $this ->password); $this ->conn-> exec ( "set names utf8" ); } catch (PDOException $exception ){ echo "Connection error: " . $exception ->getMessage(); } return $this ->conn; } }
?>
-------------
Prepared Statements in MySQLi
-------------------------------------
<?php
----------------
|
Comments
Post a Comment