vtex-apps / login

Login docs and messages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Database connection in PHP

AndresTM20 opened this issue · comments

To establish a database connection in PHP, you can use the mysqli or PDO extensions. Here's an example of how to connect to a MySQL database using the mysqli extension:
`<?php
$host = "localhost"; // The database server hostname
$user = "username"; // The database username
$pass = "password"; // The database password
$dbname = "database_name"; // The name of the database

// Create a new mysqli object and establish a connection to the database
$conn = new mysqli($host, $user, $pass, $dbname);

// Check if the connection was successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";
?>
`
In this example, you'll need to replace the values of $host, $user, $pass, and $dbname with the actual values for your database server.

The mysqli extension provides a set of functions that you can use to interact with the database. For example, you can use the mysqli_query function to execute SQL queries:
`$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "
";
}
} else {
echo "0 results";
}

mysqli_close($conn); // Close the database connection

This example executes a SELECT query to retrieve data from a users table, and then loops through the result set to output the data. Note that you should always close the database connection using the mysqli_close function after you're done using it.

Alternatively, you can also use the PDO extension to establish a database connection. Here's an example

`<?php
$host = "localhost"; // The database server hostname
$user = "username"; // The database username
$pass = "password"; // The database password
$dbname = "database_name"; // The name of the database

// Create a new PDO object and establish a connection to the database
$dsn = "mysql:host=$host;dbname=$dbname";
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$conn = new PDO($dsn, $user, $pass, $options);
echo "Connected successfully";
} catch(PDOException $e) {
die("Connection failed: " . $e->getMessage());
}

// Execute a SELECT query using the PDO object
$sql = "SELECT * FROM users";
$stmt = $conn->query($sql);

if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch()) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "
";
}
} else {
echo "0 results";
}

$conn = null; // Close the database connection
?>
`
In this example, you'll need to replace the values of $host, $user, $pass, and $dbname with the actual values for your database server. The PDO extension provides a set of classes and methods that you can use to interact with the database. The try...catch block is used to handle any errors that may occur when establishing a connection to the database or executing a query. Note that you should always close the database connection using the $conn = null statement after you're done using it.