-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(function): add mysql example in PHP
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# MySQL RDB and PHP functions | ||
|
||
A small example of a PHP function that creates a table in a MySQL database and inserts a row into it. | ||
|
||
## Deploying | ||
|
||
1. Create a MySQL database from the Scaleway console. | ||
1. Make sure to create a user as well. | ||
2. Create a new function namespace and a PHP function running using PHP 8.2 | ||
3. Copy the contents of `handler.php` into the function code editor. | ||
4. Add the following environment variables to the function: | ||
- `MYSQL_HOST`: The hostname of the MySQL database | ||
- `MYSQL_USER`: The username of the MySQL user | ||
- `MYSQL_PASSWORD`: The password of the MySQL user | ||
- `MYSQL_DB`: The name of the MySQL database | ||
5. Deploy the function. | ||
6. Grab the function URL and use it to call the function. | ||
|
||
## Usage | ||
|
||
Call the function with a body to insert a row into the table. | ||
|
||
```console | ||
curl -X POST -d '{"email": "[email protected]"}' <function-url> | ||
Inserted [email protected] into the database | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
function insertRecord($mysqli, $tableName, $data) { | ||
// Check if mysqli object is null | ||
if ($mysqli === null) { | ||
die("Database connection is not established."); | ||
} | ||
|
||
// Create table if it doesn't exist | ||
$createTableSql = "CREATE TABLE IF NOT EXISTS `$tableName` ( | ||
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, | ||
email VARCHAR(50), | ||
reg_date TIMESTAMP | ||
)"; | ||
|
||
if ($mysqli->query($createTableSql) === FALSE) { | ||
die("Error creating table: " . $mysqli->error); | ||
} | ||
|
||
// Prepare SQL statement | ||
$stmt = $mysqli->prepare("INSERT INTO $tableName (email, reg_date) VALUES (?, NOW())"); | ||
if ($stmt === false) { | ||
die("Error preparing statement: " . $mysqli->error); | ||
} | ||
|
||
// Bind parameters | ||
$stmt->bind_param("s", $data['email']); | ||
|
||
// Execute statement | ||
if ($stmt->execute() === false) { | ||
die("Error executing statement: " . $stmt->error); | ||
} | ||
|
||
echo "Record inserted successfully"; | ||
$stmt->close(); | ||
} | ||
|
||
function handle($event, $context) { | ||
$dbHost = getenv('MYSQL_HOST'); | ||
$dbUsername = getenv('MYSQL_USER'); | ||
$dbPassword = getenv('MYSQL_PASSWORD'); | ||
$dbName = getenv('MYSQL_DB'); | ||
|
||
$mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); | ||
$tableName = "users"; | ||
|
||
if ($mysqli === null) { | ||
die("Database connection is not established."); | ||
} | ||
|
||
if ($mysqli->connect_error) { | ||
die("Connection failed: " . $mysqli->connect_error); | ||
} | ||
|
||
// Extract user data from the request | ||
$data = json_decode($event['body'], true); | ||
if ($data === null || $data['email'] === null) { | ||
return [ | ||
"body" => "Email is required", | ||
"statusCode" => 400, | ||
]; | ||
} | ||
|
||
try { | ||
insertRecord($mysqli, $tableName, $data); | ||
} catch (Exception $e) { | ||
return [ | ||
"body" => "Error: " . $e->getMessage(), | ||
"statusCode" => 500, | ||
]; | ||
} | ||
|
||
return [ | ||
"body" => "Inserted " . $data['email'] . " into the database", | ||
"statusCode" => 200, | ||
]; | ||
} |