I’ve taken the advice given to move to PDO and have used pdo to connect to my database which is working. I have used FETCH_UNIQUE and other parts to display the text from the database. For testing purposes I have four entries in the pageheader / pagecontent of the database. I see the four ‘menu’ headers (home, news, timeline, prehistoric Egypt). When I click the home page, its contents are show, this is the same for prehistoric Egypt. However, news and timeline do not behave as a link, that is there is no change in pointer, no localhost/dynamicsite/index1.php?id=2 or 3 when I hover over news and timeline. As I mentioned the others links work.
I used the var_dump($data); and got the following:
array(4) { [1]=> array(4) { [“pageheader”]=> string(4) “Home” [0]=> string(4) “Home” [“pagecontent”]=> string(21) “This is the home page” [1]=> string(21) “This is the home page” } [2]=> array(4) { [“pageheader”]=> string(4) “News” [0]=> string(4) “News” [“pagecontent”]=> string(15) “Some news stuff” [1]=> string(15) “Some news stuff” } [3]=> array(4) { [“pageheader”]=> string(8) “Timeline” [0]=> string(8) “Timeline” [“pagecontent”]=> string(10) “Some links” [1]=> string(10) “Some links” } [4]=> array(4) { [“pageheader”]=> string(17) “Predynastic Egypt” [0]=> string(17) “Predynastic Egypt” [“pagecontent”]=> string(22) “Predynastic Egypt test” [1]=> string(22) “Predynastic Egypt test” } }
In case it matters I will include the database connection and the code for the index1.php page.
The connection:
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=scarabbeetle", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
The index1,php:
<?php
require_once("db1.php");
$data = array();
$sql = $conn->query("SELECT * FROM pages");
$data = $sql->fetchAll(PDO::FETCH_UNIQUE);
var_dump($data);
$id = (isset($_GET['id']) && array_key_exists($_GET['id'],$data) ? $_GET['id'] : 1);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>test</title>
<link href="css/pagestyles.css" rel="stylesheet">
</head>
<body>
<?php include("includes/top.php") ?>
<nav class="nav">
<span class="nav__list">
<?php
foreach($data as $rid => $row):
echo '<a href="index1.php?id='.$rid.'">'.$row['pageheader'].'</a>';
endforeach;
?>
</span>
</nav>
<hr>
<?php echo $data[$id]['pageheader']; ?>
<hr>
<?php echo $data[$id]['pagecontent']; ?>
<div class="space"></div>
<!-- footer content -->
<?php include('includes/footer.php'); ?>
</body>
</html>
I don’t understand why the news / timeline links are not links. Any help will be appreciated.