Page 1 of 1

Error with PDO object

Posted: Fri Apr 25, 2008 2:17 pm
by highjo
hello!
Since this morning i'm working on a applcation that send sms through the net.I'm using ajax with php for the validation.
If the data is submitted and there is no error it should direct to a page.that page script inserts the data in the database. for that i've set a class that provide all the functions for that.Now the problem is any time i run it i see this:
Unexpected error occuredSQLSTATE[28000] [1045] Access denied for user 'ODBC'@'localhost' (using password: NO)
Fatal error: Call to a member function prepare() on a non-object in C:\Program Files\xampp\htdocs\servDTxtBuddy\handler.class.php on line 31
i'ved change things and up to now nothing and i bet you it's king of frustrating.I'm using XAMPP 1.5.5 and pdo extensions are enabled
here is the page

Code: Select all

[
<?php
session_start();
include_once('config.php');
include_once("handler.class.php");
$handler = new handler();
$handler->insertData();
 
 ?>
 
 
 
<!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=iso-8859-1" />
<title>Wlecome to Authentication page</title>
</head>
 
<body>
 
<h1>Authentication</h1>
</body>
</html>
 
 
and the class script handler.class.php is

Code: Select all

<?php
//include_once("config.php");
class handler
{
     private $txtID = 0;
    
    function dbconnect()
    {
        echo $db_user;
        $dsn = "mysql:dbname=$db_database;host=$db_hostname";
        try
        {
            $pdo = new PDO($dsn, $db_user, $db_password);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $pdo;
        }
        catch(Exception $e)
        {
            echo "Unexpected error occured" . $e->getMessage();
        }
    }
    //insert data when the iwalletsettings page is called
    function insertData()
    {
        $txtID++;
        $db = $this->dbconnect();
        $sql = "insert txtbuddydetails textId ,senderId, recipientNumber, txtMessage  values(:txtID, :sendID, :reNumber, :txtSMS)";
        try
        {
            $_SESSION['ID'] = $txtID;
            $stmt = $db->prepare($sql);
            $stmt->binParam(':txtID', $txtID );     
            $stmt->binParam(':sendID', $_SESSION['text']['txtMessage'] = $_POST['txtMessage']);
            $stmt->binParam(':reNumber', $_SESSION['text']['txtNumber'] = $_POST['txtNumber']);
            $stmt->binParam(':txtSMS', $_SESSION['text']['txtMessage'] = $_POST['txtMessage']);
            $stmt->execute();
        }
        catch(Excetion $e)
        {
            echo ("Unexpected error Occured" . $e.getMessage());
        }
    }
    
    function updateStatus($sendStatus)
    {
        $dbUpdate = $this->dbconnect();
        //$sendStatus is the response from the SMS Gateway API
        if($sendStatus == 1)
        {
            try
            {
                $sqlstatus = "update txtbuddydetails set status = 1 where textId= :uID ";
                $stmtupdate = $dbUpdate->prepare($sqlstatus);
                $stmtupdate->bindParam(":tID", $_SESSION['ID']);
                $stmtupdate->execute();
            }
            catch(Exception $e)
            {
                echo ("Unexpected error Occured" . $e.getMessage());
            }
            
        }
        else
        {
            try
            {
                $sqlstatus = "update txtbuddydetails set status = 2 where textId= :uID ";
                $stmtupdate = $dbUpdate->prepare($sqlstatus);
                $stmtupdate->bindParam(":uID", $_SESSION['ID']);
                $stmtupdate->execute();
            }
            catch(Exception $e)
            {
                echo ("Unexpected error Occured" . $e.getMessage());
            }
        }
    }
    
    function removeInvalidTransaction()
    {
        try{
            $dbRemove = $this->dbconnect();
            $sqlstatus = "update txtbuddydetails set status = 2 where textId= :reID ";
            $stmtremove = $dbRemove->prepare($sqlstatus);
            $stmtremove->bindParam(":reID", $_SESSION['ID']);
            $stmtremove->execute();
            $_SESSION['ID'] = " ";
            $txtID--;
        }
        catch(Exception $e)
        {
            echo ("Unexpected error Occured" . $e.getMessage());
        }
    }
    
    
    function retrieveData()
    {
        try
        {
            $dbretrieve = $this->dbconnect();
            $sqlretrieve = "select senderId, recipientNumber, txtmessage from txtbuddydetails where textId = :rID";
            $stmtretrieve = $dbretrieve->prepare($sqlretrieve);
            $stmtretrieve->bindParam(":rID",$_SESSION['ID']);
            $stmtretrieve->execute();
            
            $data = $stmtretrieve->fetch(PDO::FETCH_ASSOC);
        }
        catch(Exception $e)
        {
            echo ("Unexpected error Occured" . $e.getMessage());
        }
    }
}
 
?>
please give me some clues :banghead:

Re: Error with PDO object

Posted: Fri Apr 25, 2008 3:09 pm
by EverLearning
I think the problem is in this line(handler.inc.php: line 27)

Code: Select all

$sql = "insert txtbuddydetails textId ,senderId, recipientNumber, txtMessage  values(:txtID, :sendID, :reNumber, :txtSMS)";
It should be

Code: Select all

$sql = "
INSERT INTO txtbuddydetails 
    (textId ,senderId, recipientNumber, txtMessage)  
    VALUES
    (:txtID, :sendID, :reNumber, :txtSMS)";
You need to enclose field names in brackets

Code: Select all

(textId ,senderId, recipientNumber, txtMessage)  
and to separate VALUES from staring bracket with field values

Code: Select all

VALUES
    (:txtID, :sendID, :reNumber, :txtSMS)

Re: Error with PDO object

Posted: Fri Apr 25, 2008 3:28 pm
by highjo
i've corrected mikates but still it posting the same
Unexpected error occuredSQLSTATE[28000] [1045] Access denied for user 'ODBC'@'localhost' (using password: NO)
Fatal error: Call to a member function prepare() on a non-object in C:\Program Files\xampp\htdocs\servDTxtBuddy\handler.class.php on line 32
I thought it is not reading the config.php because the pdo sqlsate says : Access denied and i echo the $db_users in the file to check and the post i the same with root at the beginning
rootUnexpected error occuredSQLSTATE[28000] [1045] Access denied for user 'ODBC'@'localhost' (using password: NO)
Fatal error: Call to a member function prepare() on a non-object in C:\Program Files\xampp\htdocs\servDTxtBuddy\handler.class.php on line 32
here is is rigth place to include the config file?in the class scipt or in the web page?

Re: Error with PDO object

Posted: Fri Apr 25, 2008 11:37 pm
by EverLearning
The thing is that your variables defined in config.inc.php are not visible inside the $handler->dbconnect(), which is why you're getting the

Code: Select all

Access denied for user '[color=#FF0000]ODBC[/color]'@'localhost'
So you can declare them global inside the dbconnect() method or pass them as parameters to the same method, like this

Code: Select all

// your page where you instantiate handler class
$handler->dbconnect($db_hostname, $db_database, $db_user, $db_password);
 
// and change the dbconnect signature to have arguments in handler.class.php
function dbconnect($db_hostname, $db_database, $db_user, $db_password) {
 
// .. your code
 
}
 
 

Re: Error with PDO object

Posted: Sat Apr 26, 2008 11:48 am
by RobertGonzalez
Your connection string is jacked up. Unless your mysql user name is ODBC.

You are not even getting to the point where you query can fail for bad syntax. You are actually not even connecting to the database.

Re: Error with PDO object

Posted: Sun Apr 27, 2008 11:52 am
by highjo
got it! ithink i'll try ever learning's advice and let u know if it solve it.I think i like this forums :)

Re: Error with PDO object

Posted: Mon Apr 28, 2008 6:15 am
by highjo
Hi! i solved my problem.it's due to the fact that my dbconnect() function is not accessing the parameters included. For those who may encounter similar problem, i would say that OBDC is the default user in mysql (from what i've read) and then if the connection object user and stuff is not defined or having issues ,the mysql server would suppose that the connection is done with the default user.Thanks Again guys! :drunk: