1

I red several questioins, but no one helped.
Fatal error: Call to a member function bind_param() on boolean in -> nope.
Fatal error: Call to a member function prepare() on null -> nope.
Fatal error: Call to a member function count() on boolean -> nope.
Fatal error Call to a member function prepare() on null -> nope.
fatal error call to a member function prepare() on resource -> nope.
Error: Call to a member function prepare() on a non-object -> nope. I am done..

I am using PHP5 and mySql with PDO:

Connection and Select works fine, but the Insert didnt want to work.
That's my function:

function AddNewUser($nickname, $email)
{
    ini_set('display_errors', 1); //DELETE ME
    ini_set('expose_php', 1); //DELETE ME

    $pdo = EstablishDBCon();
    echo "Subscribe user..<br/>";

    $sql = "INSERT INTO db.table (nickname, email, insertdate, updatedate) VALUES (:nickname, :email, :insertdate, :updatedate)";

    try {
        $stmt = $pdo->prepare($sql); //Error at this line
        //id?
        $stmt->bindParam(':nickname', $nickname, PDO::PARAM_STR);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR);
        $stmt->bindParam(':insertdate', date("Y-m-d H:i:s"), PDO::PARAM_STR);
        $stmt->bindParam(':updatedate', null, PDO::PARAM_NULL);
        $stmt->exeute();

        CloseDBCon($pdo);
        echo "Subscribed!<br/>";
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
}

The DB pattern is:
id (int not null auto_inc) | nickname (varchar not null) | email (varchar not null) | insertdate (datetime) | updatedate (datetime)

I am new to php and I do not understand that type of error. I marked the line inside the code, where the error is thrown:

$stmt = $pdo->prepare($sql); //Error at this line

Can someone help me?

Thanks in advance!

//EDIT: Connection aka db_connection.php:

<?php
echo 'Establishing MySQL Connection<br/>';

$pdo = null;
$dsn = 'mysql: host=xx; dbname=xx';
$dbUser = 'xx';
$pw = 'xx';

try {
    $pdo = new PDO($dsn, $dbUser, $pw);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo 'Connection established.<br/>';
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

return $pdo;
?>

Here is the EstablishDBCon function:

function EstablishDBCon()
{
    $pdo = include_once 'db_connection.php';
    return $pdo;
}
Community
  • 1
  • 1
Ismoh
  • 1,074
  • 2
  • 12
  • 35
  • 3
    Show your `EstablishDBCon` function – jszobody Jun 13 '16 at 19:39
  • 2
    Your code was not able to connect to the database, so $pdo is `false`. Check your error log, and put some error checking into your `EstablishDBCon` class. – aynber Jun 13 '16 at 19:39
  • If you `var_dump($pdo);` then you will see that it is a boolean datatype with either a TRUE or FALSE value. Not sure what the `__construct()` of `EstablishDBCon()` does but I have a funny feeling that it is **NOT** returning a connection handle. – MonkeyZeus Jun 13 '16 at 19:42
  • your connection failed, you obviously have NO error handling to detect this, and blindly blundered onwards using a bad connection handle (php boolean false). – Marc B Jun 13 '16 at 19:44
  • 1
    @MonkeyZeus: pdo will NEVER return a boolean true for a connection. you get a pdo object, or a boolean false. – Marc B Jun 13 '16 at 19:44
  • question is edited. – Ismoh Jun 13 '16 at 19:57
  • 2
    `include_once` is for including code into your file. It's a control structure, and as such, does not return objects. Depending on your db_connection.php, try including it at the top of the file, outside of any functions. – aynber Jun 13 '16 at 20:00
  • Thank you aynber. Is there a different include function which returns a object, so that i can save the credentials in one file or method to not write it down more than once. – Ismoh Jun 13 '16 at 20:56
  • Put your `EstablishDBCon` inside of your `db_connection.php`. If your function is created properly, you can use the `include_once` at the top of each script and it will be useable everywhere. – aynber Jun 13 '16 at 21:08
  • Ah. Thank you. you should write an answer :) – Ismoh Jun 13 '16 at 21:09
  • I shall. I was just thinking it through... – aynber Jun 13 '16 at 21:10

3 Answers3

1

The best way to reuse functions is to put it inside of the include file, then include it at the top of each file you'll need it. So inside of your db_connection.php, create your function:

function EstablishDBCon()
{
    $pdo = false;
    try{ 
         // Put your PDO creation here
    } catch (Exception $e) {
        // Logging here is a good idea
    }
    return $pdo;

}

Now you can use that function wherever you need it. Make sure you always make sure $pdo !== false before you use it, to make sure your connection hasn't failed.

aynber
  • 22,380
  • 8
  • 50
  • 63
1

The problem is in the function EstablishDBCon(), which expects the include_once statement to return a value as if the contents of the included file are a function.

function EstablishDBCon()
{
    $pdo = include_once 'db_connection.php';
    return $pdo;
}

But that's not how include_once works here:

if the code from a file has already been included, it will not be included again, and include_once returns TRUE.

That's why you end up with TRUE (a boolean) in your $pdo variable.

In any event, this kind of construction makes your code really hard to follow. I recommend only using include and friends to combine self-contained PHP functions together, or to embed parts of HTML pages in one another.

alexis
  • 48,685
  • 16
  • 101
  • 161
0

Call to a member function on boolean in this case means that $pdo is not an object, it's a boolean. So it's likely that EstablishDBCon() is returning either a true on success or false otherwise, as opposed to a database resource. Double-check the docs on that function. Here's a link to some relevant documentation on PDO that you'll need.

ShaneOH
  • 1,454
  • 1
  • 17
  • 29