Connecting to MySql using PHP

connecting to mysql using mysql_connect() has been depreciated

so now using mysqli() I am able to connect to mysql database.

since it is object orientated programming we can utilize features such as $con->query() instead of calling mysql_query($sql,$con) and you can pass the database info inside mysqli() instead of using mysql_select_db()

<?php
error_reporting(0);
if(!empty($_POST))
{
if(isset($_POST[‘name’],$_POST[’email’],$_POST[‘phone_number’],$_POST[‘job_requested’]))

{
$name  = trim($_POST[‘name’]);
$email = trim($_POST[’email’]);
$phone = trim($_POST[‘phone_number’]);
$job   = trim($_POST[‘job_requested’]);
$today = date(“F j, Y, g:i a”);
}
}

$con = new mysqli(‘dbLocation’,’username’,’password’, ‘databasename’);
if(!empty($name) && !empty($email) && !empty($phone) && !empty($job))
{
$sql=”INSERT INTO table (name, email,phone_number,job_requested,DateRequested)
VALUES
(‘$_POST[name]’,’$_POST[email]’,’$_POST[phone_number]’,’$_POST[job_requested]’,’$today’)”;

if($result = $con->query($sql) or die(“Sorry could not connect to database because ” . $con->error))
{
//create a success event

send email event

$to = “email@email.com”;
$subject = “A job have been requested”;
$message = “On ” . $today . ” a job request for ” . $job . ” from ” . $name . ” was placed. Please email them at ” . $email . ” or call them at ” . $phone;
$headers = “From:” . $email;
mail($to, $subject, $message,$headers);
}
}
?>