Hi!
Im trying to select data from a file via PHP.
This is the code in the first file:
<?php header("location: http://webbred2.utb.hb.se/~s163007/uppgift-read.php"); ?>
<?php
$servername = "localhost";
$username = "username";
$password = "****";
$dbname = "dbname";
try{
$sql = "INSERT INTO Book(Book_id, Author, Title)
VALUES ('Karin Boye', 'Kris')";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Database connected successfully. "; } catch(PDOException $e){ echo "Connection failed: " . $e->getMessage(); } ?>
And this is the code in the second file:
<!DOCTYPE html>
<?php
$servername = "localhost";
$username = "username";
$password = "*****";
$dbname = "dbname";
try {
$conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT Author, Title FROM Book");
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row){
echo $row ['Author'];
echo $row ['Title'];
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</body>
I want to select the data and show the values in a table but I don’t seem to find what Im doing wrong…