I am trying to store the rows of data from the database into an empty array. I want to make use of the pdo object as well but I am stuck and don’t understand how to do this.
Thanks in advance.
Here is my php code:
function db_connect() {
try {
$servername = DBHOST;
$databasename = DBNAME;
$user = DBUSER;
$password = DBPASS;
$pdo = new PDO("mysql:host=$servername;dbname=$databasename",$user,$password);
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
catch (PDOException $e)
{
die($e->getMessage());
}
}
// Handle form submission
function handle_form_submission() {
global $pdo;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$email = $_POST['email'];
$mood = $_POST['mood'];
$comments = $_POST['comment'];
$date = date('Y-m-d');
$stmt = $pdo->prepare("INSERT INTO comments (email,mood,commentText,date) VALUES(:email,:mood,:commentText,:date)");
$stmt->bindParam(':email',$email);
$stmt->bindParam(':mood',$mood);
$stmt->bindParam(':commentText',$comments);
$stmt->bindParam(':date',$date);
$stmt->execute();
}
}
// Get all comments from database and store in $comments which is an array
function get_comments() {
global $pdo;
global $comments;
//TODO
$sql = $pdo->query("SELECT * FROM comments ORDER BY ID DESC");
/*while($comments = $sql->fetch()){
echo $comments['date']."<b />".$comments['mood']."<br />".$comments['email'];
}*/
$sql->execute();
}