I am trying to call a record from a table into html to create an admin page so the content can be updated. I cannot get the record to come up. I am totally new to this so any help is appreciated. My table name is tblContent and my database name is data1. I have only one row in the table with a PageID of 1.
home.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo mysql_result($rs,0,”PageTitle”);?></title>
<link rel="stylesheet" type="text/css" href="origstyle.css">
</head>
<body>
<div id="container">
<h1>Site Heading</h1>
<?php
require_once 'classes.php';
try {
$conn = new PDO("mysql:host=$host;db_name=$db_name", $db_username, $db_password);
$rs = mysql_query("getData");
echo mysql_result($rs,0,”Content”);
} catch (PDOException $pe) {
die("Could not connect to the database $db_name :" . $pe->getMessage());
}
?>
</div>
</body>
</html>
classes.php
<?php
//Enter your database connection details here.
$host = 'localhost'; //HOST NAME.
$db_name = 'XXXXdbName'; //Database Name
$db_username = 'XXXXuserName'; //Database Username
$db_password = 'XXXXpass'; //Database Password
try
{
$pdo = new PDO('mysql:host='. $host .';dbname='.$db_name, $db_username, $db_password);
}
catch (PDOException $e)
{
exit('Error Connecting To DataBase');
}
class database
{
function __construct($pdo)
{
$this->pdo = $pdo;
}
function getData()
{
$query = $this->pdo->prepare('SELECT * FROM data1');
$query->execute();
return $query->fetchAll();
}
}
?>
There's quite a lot of little issues that stop this code from working.
The biggest is that you are trying to use two different database libraries. There's the deprecated mysql
library which has all the functions starting with mysql_
and then there's PDO. Replace all the mysql
functions with their PDO
counterparts.
The next thing I notice is that you seem to be thinking that you pass a method name to the querying function (mysql_query"myData"
), but you do not. You call the method on an instance of the class. First you create an instance, like you did for the PDO object, and then you call a method.
$database = new database()
$rs = $database->getData()
The third thing I noticed is that you are creating the PDO object twice. There's no need to create the instance in home.php
, so just get rid of that (along with the try/catch stuff related to it).
The fourth thing is minor. You should also capitalize your class names.
Put these together, and you get something like this:
home.php:
require_once "classes.php";
$rs = $database->getData();
// Code that turns $rs into HTML
// Sorry, I don't know PDO well enough to
// say what that is.
// I do know that a simple echo will not suffice.
classes.php:
$host = "localhost"
$db_name = "";
$db_username = "";
$db_password = "";
class Database {
function __construct($pdo) {
$this->pdo = $pdo;
}
function getData () {
$query = $this->pdo->prepare('SELECT * FROM data1');
$query->execute();
return $query->fetchAll();
}
}
try {
$pdo = new PDO('mysql:host='. $host .';dbname='.$db_name, $db_username, $db_password);
} catch (PDOException e) {
exit('Error Connecting To DataBase');
}
$database = new Database($pdo);