To establish a connection to a database using PDO it is necessary to define the connection string and pass it to a PDO instance, as exemplified below:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "banco";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// modo de erro do PDO para gerações de exceções
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE tabela SET lat='$lat1', lon='$lon1', info='$info1', hora='$hora1' WHERE id='$id1'";
$stmt = $conn->prepare($sql);
// executa a query
$stmt->execute();
// mensagem de sucesso
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
In the definition of the connection string with that database, you must first define the type of the database (in this case MySQL), the data for authentication in the database (user and pass), in addition to the database to which you want to establish a connection (in this case "bank"). In the presented script, the PDO error mode was also defined for exception generations whenever any errors occur with the executions of queries in the database.
Whenever a script is terminated, the connection is automatically terminated. If you need to terminate the connection before, just use the following statement: $conn = null;