0

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!

3
Contributors
8
Replies
42
Views
3 Years
Discussion Span
Last Post by Ryan_12
0

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');

Edited by veedeoo: info added.

0

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();
    }

?>

Edited by Ryan_12

0

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;

Edited by veedeoo: info added.

0

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?

Edited by Ryan_12

0

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);

Edited by veedeoo: info added.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.