<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Change character set to utf8
mysqli_set_charset($con,"utf8");
return $con;
mysqli_close($con);
?>
<?php
class my_db{
private $db;
public function __construct( ){
$this->db=new mysqli('localhost', 'user', 'psw', 'dbname') or die(mysqli_connect_error());
$this->db->set_charset('utf8');
}
public function readProductById($productId){
$result = $this->db->query("select * from products where id =$productId");
if( $result->num_rows>0){
return $result->fetch_assoc();
} return false;
}
}
$mydb = new my_db();
$product = $mydb->readProductById(1);