Enieber Cunha
My name is Enieber Cunha and I living in Brazil. I am developer JS and Android and in love for User Exprience, functional programming and classical music.
<?php
$driverDB = 'mysql';
$host = 'localhost';
$dbName = 'test';
$user = 'root';
$pass = '123';
$dbOptions = "$driverDB:host=$host;dbname=$dbName;charset=UTF-8";
$conn = new PDO($dbOptions, $user, $pass);
echo "this is $conn";
<?php
use Connection;
public function getPost($idPost)
{
try
{
$database = new Connection();
$db = $database->openConnection();
$sql = "SELECT * FROM post WHEN id=$idPost" ;
foreach ($db->query($sql) as $row) {
echo " ID: ".$row['ID'] . "<br>";
echo " Title: ".$row['title'] . "<br>";
echo " Post: ".$row['post'] . "<br>";
echo " Author: ".$row['author'] . "<br>";
}
}
catch (PDOException $e)
{
echo "Erro ao tentar selecionar Post: " . $e->getMessage();
}
}
<?php
// codigo de conecção com banco
function insertPost($conn, $title, $post, $author)
{
try
{
$conn = new Connection();
$db = $conn->openConnection();
$stm = $db->prepare("INSERT INTO post (title, post, author)
VALUES (:post, :title, :author)") ;
$query = array(
':title' => $title,
':post' => $post,
':author' => $author
);
$stm->execute($query);
echo "Novo Post Criado";
} catch (PDOException $e)
{
echo "Ocorreu um erro ao tentar adicionar o post: " . $e->getMessage();
}
}
<?php
// codigo de conecção com banco
public function updatePost($idPost, $title, $post, $author)
{
try
{
$conn = new Connection();
$db = $conn->openConnection();
$sql = "UPDATE `post` SET `title`= $title, `post` = $post , `author` = $author WHERE `id` = $idPost";
$affectedrows = $db->exec($sql);
if(isset($affectedrows))
{
echo "Post atualizado com sucesso!";
}
}
catch (PDOException $e)
{
echo "Erro ao tentar atualizar o post: " . $e->getMessage();
}
}
<?php
// codigo de conecção com banco
public function deletePost($idPost)
{
try
{
$conn = new Connection();
$db = $conn->openConnection();
$sql = "DELETE FROM post WHERE `id` = $idPost";
$affectedrows = $db->exec($sql);
if(isset($affectedrows))
{
echo "Post apagado com sucesso";
}
catch (PDOException $e)
{
echo "Ocorreu um erro ao tentar apagar o post: " . $e->getMessage();
}
}
}
By Enieber Cunha
My name is Enieber Cunha and I living in Brazil. I am developer JS and Android and in love for User Exprience, functional programming and classical music.