Tag: PHP

Database connection in PHP with PDO example

Use the following PHP to connect to your MySQL database using PDO. $db = new PDO(‘mysql:host=localhost;dbname=testdb;charset=utf8mb4’, ‘username’, ‘password’); Replace localhost, testdb, username and password with your own details. Select rows in a table In this example we are going to select the firstname and lastname from a table called members. foreach($db->query(‘SELECT * FROM members’) as…

Continue reading Database connection in PHP with PDO example

How to show PHP errors correctly

To turn on PHP error reporting, paste the following code to the top of your PHP file. ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1); error_reporting(E_ALL); Detailed explanation: // Turn off all error reporting error_reporting(0); // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch…

Continue reading How to show PHP errors correctly

Database connection in PHP with MySQLi examples

Use the following PHP block to connect to your database with mysqli function db_connect($host,$user,$pass,$db) { $mysqli = new mysqli($host, $user, $pass, $db); $mysqli->set_charset(“utf8”); if($mysqli->connect_error) die(‘Connect Error (‘ . mysqli_connect_errno() . ‘) ‘. mysqli_connect_error()); return $mysqli; } $mysqli = db_connect(‘localhost’,’username’,’password’,’database’); Replace localhost, username, password and database with your own details. Select rows in a table Here’s…

Continue reading Database connection in PHP with MySQLi examples