Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:39:26+00:00 2026-06-10T18:39:26+00:00

When I used mysql_* functions for database operations, I used to put the connecting

  • 0

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.

enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:39:28+00:00Added an answer on June 10, 2026 at 6:39 pm

    Your helper functions are trying to access the global variable $db, which is not in scope. This error can be fixed with global $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 $db variable will become either “part of” or encapsulated in the class, which is a worthy goal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am converting some of my code that used ext/mysql ( mysql_*() functions) to
I have a file sess_function.php This file lists the various (6) functions used to
I use the following procedure to call the functions within the pages of my
For a project where most queries on the database will include requirements like within
Tech used: MySQL 5.1 and PHP 5.3 I am just designing a new database
Here I am sharing code of two functions. Both return an instance of a
I'm writing PHP code that uses a database. To do so, I use an
I've used MySQL (via PHPMyAdmin) a lot before but never really understood half of
Two libraries for Mysql. I've always used _mysql because it's simpler. Can anyone tell
I want to use mysql with a rails app (I have never used mysql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.