Hey everyone!

I've created my db.php and all my other scripts are fine.. just when I created a new page and included db.php I created a function. In the function I used my $dbh variable and it is throwing an error...

Notice: Undefined variable: dbh in C:\xampp\htdocs\Crafting Bench Server Website\inc\getinfo.php on line 6
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\Crafting Bench Server Website\inc\getinfo.php on line 6

Here is my getinfo.php:

<?php
    try {
        require('db.php');

        function getInfo($user_row, $mcusername) {
            $SQL = $dbh->prepare('SELECT * FROM users WHERE mcusername=:mcusername');
            $SQL->bindValue(':mcusername', $mcusername, PDO::PARAM_STR);
            $SQL->execute();
            $user_row = $SQL->fetchColumn();
            return $user_row;
        }
    } catch (PDOException $e) {
        $e->getMessage();
    }
?>

and well my db.php already works so if you need that let me know :D

I hope somebody can help me! I tried setting the $dbh to be global, but that didn't remove the fatal error warning and is generally just very bad practice in php, cheers!

The error tells us that the $dbh is a non-object.

Where is the part of the code where it says something like this

   $dbh = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 'username', 'password');

This is my db.php:

<?php
    $dsn = 'mysql:host=localhost;dbname=hidden';
    $username = 'root';
    $password = '';

    try {
        $dbh = new PDO($dsn, $username, $password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo $e->getMessage();
    }

?>
Member Avatar for diafol

pass the $dbh object to the function as a parameter?

I tried that, but its not recognising the $dbh object

try something like this

function getInfo($dbh,$user_row, $mcusername){

if($dbh){

try{

## put your stuffs here
    return $user_row;
}
    catch (PDOException $e) {
        echo $e->getMessage();
    }


}


}

do not wrap the entire function with try because there is no error to catch until the function is called. Besides the error will be occuring inside the function if there is any.

double check your usage of this

$user_row;

It still doesnt work :(

Warning: Missing argument 1 for getInfo(), called in C:\xampp\htdocs\Crafting Bench Server Website\profile-user.php on line 101 and defined in C:\xampp\htdocs\Crafting Bench Server Website\inc\getinfo.php on line 3

Notice: Undefined variable: dbh in C:\xampp\htdocs\Crafting Bench Server Website\inc\getinfo.php on line 4

Is it something wrong with the variable scope?

let's retrace all these..

try this first

<?php

$dsn = 'mysql:host=localhost;dbname=hidden';
$username = 'root';
$password = '';
try {
$dbh = new PDO($dsn, $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}

if($dbh){

    echo 'connected<br/>';
}

try viewing the page on your borwser and see if it is working. If it is, include this file to this

<?php

    include_once('the_page_above.php');

    function test($dbh){

       echo ($dbh ? 'connection is persistent inside this function' : 'sorry not connected');   
   }

   $x = test($dbh);

I solved it!

I listened to what @diafol said about putting it in as a parameter

So what I did was I included my db.php on the page I wanted and everytime I used the function I entered the db.php variable $dbh :)

Thanks for all the help everyone! Much Love <3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.