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 shown 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, one must initially define the type of the database (in this case, Mysql), the data for authentication in the database (user and pass), and the database to which one wishes to establish connection (in this case "database"). In the shown script, the PDO error mode has also been set for generations of exceptions whenever any errors occur with the executions of queries in the database.
Whenever a script is finished, the connection is automatically terminated. If you need to finish the connection first, just use the following instruction: $conn = null;
thank you, it worked perfectly.
– Izakdavis Pereira Da Cunha Sou