When I used mysql_* functions for database operations, I used to put the connecting to the database and initialization code in a separate file and and include it on other pages and it worked well.
I have learned PDO recently so I thought of trying it out with PDO. So I have 4 PHP files.
config.php files stores all the database related information.
<?php
$host = "localhost";
$username = "root";
$password = "abc123";
$dbname = "blog";
?>
init.php includes and config.php file and initializes the database connection.
<?php
include_once("config.php");
$db = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
And there’s a separate file called functions.php which has all the functions. It includes the init.php file.
<?php
include_once("init.php");
function AddCategory($_name)
{
$param = $_name;
$query = $db->prepare("INSERT INTO categories SET name = :name");
$query->bindParam(':name', $param, PDO::PARAM_STR);
$query->execute();
}
function CategoryExists($_name)
{
$param = $_name;
$query = $db->prepare("SELECT COUNT(1) FROM categories WHERE name = :name");
$query->bindParam(':name', $param, PDO::PARAM_STR);
$query->execute();
$results = $query->fetch();
return ($results == '0') ? false : true;
}
?>
And the index.php file which displays a small form.
<?php
include_once("functions.php");
if(isset($_POST['name']))
{
$name = trim($_POST['name']);
if(empty($name))
{
$error = "You must enter a category name";
}
else if (CategoryExists($name))
{
$error = "That category already exists";
}
else if (strlen($name) > 24)
{
$error = "Category name can only be upto 24 characters";
}
if(!isset($error))
{
AddCategory($name);
}
}
?>
<!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>Add a Category</title>
</head>
<body>
<h1>Add a Category</h1>
<form action="" method="post">
<div>
<label for="name">Name</label>
<input type="text" name="name" value=""></input>
</div>
<div>
<input type="submit" value="Add Category"></input>
</div>
</form>
</body>
</html>
But when I enter something and submit it, it throws this error even though I have included the necessary file.

Can someone tell me what I’m doing wrong here? I’d really appreciate it.
Or is there a more easier/efficient/correct way to accomplish this?
Thank you.
Your helper functions are trying to access the global variable
$db, which is not in scope. This error can be fixed withglobal $db;at the top of each function.This approach is a natural step while you are learning, but from an engineering point of view it’s quite undesirable because your functions are dependent on global state. In the future you should look into how you can make a class that extends
PDO(or aggregates a PDO instance) and have these functions become methods of that class. If you do this then the$dbvariable will become either “part of” or encapsulated in the class, which is a worthy goal.