Jump to content

Slight error, help?


vic vance

Recommended Posts

Hey I was trying out PDO, and I got a minor error:

 

Fatal error: Call to undefined method connection::prepare()  on line 12

 

This is my connect.php class

<?php

 class connection {
    
    private $user;
    private $pass;
    
    public function __construct() {
        
        $this->user = "lala";
        $this->pass = "28282";
    }
    public function dbConnect()
    {        
        try {
        $dbh = new PDO('mysql:host=localhost;dbname=project', $this->user,  $this->pass);
        array(PDO::ATTR_PERSISTENT => true);

        } catch (PDOException $e) {
            print "<b>Connection Failed:</b> ".$e->getMessage()."";
            die();
        }
    }
}

?>

This is the index.php

<?php
error_reporting(E_ALL ^ E_NOTICE);
define( 'ROOT', "./" );
define( 'temp', "templates/" );

require_once 'connect.php';
$con = new connection();
$con->dbConnect();


$head 		= implode("", file(temp."head.html"));
echo $head;

$stmt = $con->prepare('SELECT * FROM db_members'); // error here LINE 12
$stmt->execute();

$result = $stmt->fetchAll(\PDO::FETCH_OBJ);
foreach($result as $row){
        print($row->username);
}

$foot 		= implode("", file(temp."/foot.html"));
echo $foot;

?>

I know I am connected because when I remove the connect.php class and try connecting directly it seems to work:

$con = new PDO('mysql:host=localhost;dbname=project', 'lala',  '28282'); // removed the class and directly connected

$head 		= implode("", file(temp."head.html"));
echo $head;

$stmt = $con->prepare('SELECT * FROM db_members'); // error goes away and everything works when I directly connect
$stmt->execute();

Am I calling the connect.php class wrong?

Please help, Thank you.

Edited by vic vance
Link to comment
Share on other sites

1 - no need to complicate your life and make a class out of this. Just take that simple code and made it an include file for it.

2 - you created the var $con as an object of connection type.

3 - you executed dbconnect for that object.

 

And - what?

 

You have no pdo connection variable. See how the class made things so complex?

 

$pdo = $con->dbConnect();

$stmt = $pdo->prepare(.....

 

Tip - make the dbname an argument of your dbconnect function (which is all you need) and then you won't have to have a different one for every db you have. (Assuming you use the same master id/pswd for all your dbs)

Link to comment
Share on other sites

I get what you mean, and I was forgot a return.

 

I changed this also:

 

$con = new PDO('mysql:host=localhost;dbname=project', $this->user,  $this->pass);

 

return new PDO('mysql:host=localhost;dbname=project', $this->user,  $this->pass);

 

Thank you.

Link to comment
Share on other sites

But you really should get rid of the class thing.  Adds needless complication and overhead to your code.  And it's not practical since you don't parameterize the dbname.

 

require_once($php_path."/pdo_connect_select.php");  // contains PDOConnect function
$pdo = PDOConnect("mydbname");
 
$q = "select * from table ...... ";
$qst = $pdo->prepare($q);
$qst->execute();

 

 

And the included/required file (pdo_connect_select.php) is:

function PDOConnect($dbname)
{
   $host="mysql:host=domain.com;dbname=$dbname;charset=utf8";
   $uid = "myuser";
   $pswd = "mypswd";
   Try
    {
        $mysql = new PDO($host,$uid,$pswd);
    }
    catch (PDOException $e)
    {
         echo "Fatal Error<br>Failed to connect to mysql via PDO.  PDO Error msg is:<br>".$e->getMessage();
         return false;
    }
    if (!$mysql)
    {
         echo "Failed to connect to mysql via PDO.  Error returned is: " . GetPDO_ErrorMsg($mysql);
         return false;
    }
    else
         return $mysql;
}
//*****************************
function GetPDO_ErrorMsg($pdo,$i = 2)
{
 $pdo_errinfo = $pdo->ErrorInfo();
 return $pdo_errinfo[$i];
}


 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.