To connect slim apis with database follow the below steps:
1) Setup the slim enviornment in you system using the below URL.
http://findnerd.com/account/#url=/list/publishnode/18162/
2) Create a database name "slim".
3) Create a file under SwaggerServer folder namely dbConnection.php. Put the below code in it.
/**
* Function to setup db connection
* @return object of database connection.
*/
function getConnection() {
$dbhost="localhost";
$dbuser="root";
$dbpass="admin";
$dbname="slim";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
4) Create a table name article in your "slim" database and do some entries in it.
5) In file index.php inside SwaggerServer folder, create a function using http method to get all the data of the article table.
You can use the below code:
$app->GET('/getArticles', function($request, $response, $args) {
$sql = "select * FROM articles";
try {
$db = getConnection();
$stmt = $db->query($sql);
$articles = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($articles);
}
catch(PDOException $e) {
echo json_encode($e->getMessage());
}
});
One can also get data of the single article by using the article table primary key. Example:
$app->GET('/getArticlesById/{id}', function($request, $response, $args) {
$sql = "select * FROM articles where id =". $args['id'];
try {
$db = getConnection();
$stmt = $db->query($sql);
$articles = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($articles);
}
catch(PDOException $e) {
echo json_encode($e->getMessage());
}
});
6) To save an article in database, we have to use the POST HTTP action. Example:
$app->POST('/getArticles', function($request, $response, $args) {
$title = $request->getParam('title');
$url = $request->getParam('url');
$sql = "INSERT INTO articles (`title`,`url`) VALUES ('". $title ."','". $url ."')";
echo $sql;
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("title", $title);
$stmt->bindParam("url", $url);
$stmt->execute();
$db = null;
$sMessage = "Success!!!!";
echo json_encode($sMessage);
} catch(PDOException $e) {
echo json_encode($e->getMessage());
}
});
7) To delete an article from the article table, we have to use DELETE HTTP action. Example:
$app->DELETE('/deleteArticleById/{id}', function($request, $response, $args) {
$sql = "DELETE FROM articles WHERE id=" . $args['id'];
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $args['id']);
$stmt->execute();
$db = null;
$sMessage = "Success!!!!";
echo json_encode($sMessage);
} catch(PDOException $e) {
echo json_encode($e->getMessage());
}
});
8) Create another table name user with fields id, username and password in slim database and insert some entries in it. To update the password of the entry which you want use PUT HTTP action. Example:
$app->PUT('/updatePassword', function($request, $response, $args) {
$loginUserId = $request->getParam('id');
$password = $request->getParam('password');
$sql = "UPDATE user SET password='". $password . "' WHERE id='". $loginUserId."'";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("password", $password);
$stmt->bindParam("id", $loginUserId);
$stmt->execute();
$db = null;
$sMessage = "Update Success!!!!";
echo json_encode($sMessage);
} catch(PDOException $e) {
echo json_encode($e->getMessage());
}
});
0 Comment(s)