Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PDO problem

33 views
Skip to first unread message

Hiago Vitor

unread,
Apr 11, 2015, 7:18:36 PM4/11/15
to
//-----------------index.php---------------------------------

<?php
include 'db.php';
include 'config.php';

$Data = new DataBase($database);
//$Data = new DataBase($host,$dbname,$port,$user,$password);

$Data->email = 'testes$gmail.com';
$Data->senha = 'teste5';

echo $Data->getHost().'<br>';
echo $Data->getDbname().'<br>';
echo $Data->getPort().'<br>';
echo $Data->getUser().'<br>';
echo $Data->getPassword().'<br>';
$Data->connect();


$email='lal...@gmail.com';
$pwd='lalalalalala';
$Data->insert($email,$pwd);

?>

//-------------------------------db.php--------------------------------

<?php

class DataBase{
public $email;
public $senha;

public $host;
public $dbname;
public $port;
public $user;
public $password;
//var $type;

public function __construct($database){
self::setHost($database['host']);
self::setDbname($database['name']);
self::setPort($database['port']);
self::setUser($database['user']);
self::setPassword($database['pass']);
//---- self::set_type($database['type']);
}

//--------------------------SET

public function setHost($host){
$this->host = $host;
}

public function setDbname($dbname){
$this->dbname = $dbname;
}

public function setPort($port){
$this->port = $port;
}

public function setUser($user){
$this->user = $user;
}

public function setPassword($password){
$this->password = $password;
}

//--------------------------------GET

public function getHost(){
return $this->host;
}

public function getDbname(){
return $this->dbname;
}

public function getPort(){
return $this->port;
}

public function getUser(){
return $this->user;
}

public function getPassword(){
return $this->password;
}

public function connect (){//public function connect (){
$db = new PDO( 'mysql:host='.getHost().
';dbname='.getDbname().
';port='.getPort(),getUser(),getPassword());
$query = $db->query('SELECT * FROM conta');
}

public function insert($email,$pwd){
$sql = "INSERT INTO conta (email,pass,dataCadastro) VALUES (?, ?, NOW() )";
$query = $db->prepare($sql);
$query->execute(array($email,$pwd));
}
}
?>

//----------------------- config.php----------------------------------
<?php
// $host = '127.0.0.1';
// $dbname = 'meuDb';
// $port = '3306';
// $user = 'root';
// $password = '';

DEFINE('HOST','127.0.0.1');
DEFINE('USER','root');
DEFINE('PASS','');
DEFINE('DB','meuDb');
DEFINE('TYPE','mysql');
DEFINE('PORT','3306');

$database = array("user"=>USER,"pass"=>PASS,"name"=>DB,"host"=>HOST,"type"=>TYPE,"port"=>PORT);

?>
//PROBLEM----------------------------------------------
It's appering this error, does anyone know why this is happening ?

Fatal error: Call to undefined function getHost() in /opt/lampp/htdocs/testedb/db.php on line 70


Jerry Stuckle

unread,
Apr 11, 2015, 7:59:07 PM4/11/15
to
On 4/11/2015 7:18 PM, Hiago Vitor wrote:
<snip code>


> //PROBLEM----------------------------------------------
> It's appering this error, does anyone know why this is happening ?
>
> Fatal error: Call to undefined function getHost() in /opt/lampp/htdocs/testedb/db.php on line 70
>
>

Maybe because there is no function getHost() (just a member function of
a couple of classes by that name)?

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Hiago Vitor

unread,
Apr 11, 2015, 8:12:46 PM4/11/15
to
what's the difference of function and member function?

Jerry Stuckle

unread,
Apr 11, 2015, 8:18:03 PM4/11/15
to
On 4/11/2015 8:12 PM, Hiago Vitor wrote:
> Em sábado, 11 de abril de 2015 20:59:07 UTC-3, Jerry Stuckle escreveu:
>> On 4/11/2015 7:18 PM, Hiago Vitor wrote:
>> <snip code>
>>
>>
>>> //PROBLEM----------------------------------------------
>>> It's appering this error, does anyone know why this is happening ?
>>>
>>> Fatal error: Call to undefined function getHost() in /opt/lampp/htdocs/testedb/db.php on line 70
>>>
>>>
>>
>> Maybe because there is no function getHost() (just a member function of
>> a couple of classes by that name)?
>>
>
> what's the difference of function and member function?
>

A member function is a function that is part of a class. It must be
called with an object of that class (unless it's a static member
function - but don't worry about that right now) and is used to
set/retrieve information from that object.

My suggestion is for you to study up on object oriented programming; PHP
is going that way (although not very well).
Message has been deleted

Hiago Vitor

unread,
Apr 11, 2015, 8:26:23 PM4/11/15
to
thanks, i found that if use this->getHost() work. I've studied OOP but sometimes I have some problems I will try to find a good book of OOP to learn more.

Hiago Vitor

unread,
Apr 12, 2015, 9:01:56 AM4/12/15
to
-------connect-------

public function connect (){
try{
$db = new PDO( 'mysql:host='.$this->getHost().
';dbname='.$this->getDbname().
';port='.$this->getPort(),$this->getUser(),$this->getPassword());
return $db;
}
catch(EXCEPTION $e){
echo 'Error: ' . $e->getMessage();
}
}
----------------update--------

public function update($email,$pwd,$id){
try {
$sql = "UPDATE conta SET email= ? , pass= ? WHERE id(pk)= ? ";
$query = $this->connect()->prepare($sql);
$query->execute(array($email, $pwd, $id));
}
catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
----------new problem---------
I hava another problem now update dont work, but no erros appear

Jerry Stuckle

unread,
Apr 12, 2015, 12:40:48 PM4/12/15
to
On 4/12/2015 9:01 AM, Hiago Vitor wrote:
<snip unrelated code>
>
> -------connect-------
>
> public function connect (){
> try{
> $db = new PDO( 'mysql:host='.$this->getHost().
> ';dbname='.$this->getDbname().
> ';port='.$this->getPort(),$this->getUser(),$this->getPassword());
> return $db;
> }
> catch(EXCEPTION $e){
> echo 'Error: ' . $e->getMessage();
> }
> }
> ----------------update--------
>
> public function update($email,$pwd,$id){
> try {
> $sql = "UPDATE conta SET email= ? , pass= ? WHERE id(pk)= ? ";
> $query = $this->connect()->prepare($sql);
> $query->execute(array($email, $pwd, $id));
> }
> catch(PDOException $e) {
> echo 'Error: ' . $e->getMessage();
> }
> }
> ----------new problem---------
> I hava another problem now update dont work, but no erros appear
>

You're not checking the return value from your $query->execute()
statement. I suspect you have an error in your SQL statement, and it
doesn't look like PDOStatement::execute() throws an exception (I don't
use PDO, so all I can go from is the doc).

I suspect your problem is in your WHERE clause - what is 'id(pk)'
supposed to be? Are you trying to identify the column 'id' as a primary
key? That would be unnecessary (and illegal here).

Check the return value from your $query->execute() call, and if it's
false, call $query->errorInfo() to get the error information.

P.S. You have a couple of basic logic faults in your code here, also.
When you catch an exception, the correct way to handle it is to either
correct the error or skip any processing which depends on the call
completing.

In your connect() function, you catch the exception but only display an
error message. Then you just return from the function, with no return
value. This is your first problem - the result of trying to use the
return value from connect() when it fails is undefined.

The other problem is in your update() function, you're just assuming the
connect() function works. Since you caught any possible exception in
connect(), you won't see another exception from a connection failure.

The easiest way to correct this is to just not catch the exception in
connect(). Then it will get caught in update(), and processing will
stop (you'll get the error message from the catch block here).

Other possibilities include rethrowing the error (not good because
you're really not doing anything with the error in connect()) or
returning false from connect() and testing the return value in update()
before trying to use it.

Hiago Vitor

unread,
Apr 12, 2015, 1:04:06 PM4/12/15
to
I can insert, select etc but only update dont work(than connect is working), "id(pk)" is the primary key( "id(pk)") . That's why i dont uderstand updat not working

Jerry Stuckle

unread,
Apr 12, 2015, 4:34:16 PM4/12/15
to
> I can insert, select etc but only update dont work(than connect is working), "id(pk)" is the primary key( "id(pk)") . That's why i dont uderstand updat not working
>

Your column name is "id(pk)"? This column name is invalid in most
databases.

Did you check the result of your execute() call like I suggested?

Hiago Vitor

unread,
Apr 13, 2015, 3:48:37 PM4/13/15
to
i change the id(pk) to id like u said, than i start using:

$query->bindParam(':id', $id, PDO::PARAM_INT);

because i read that standard would be "PDO::PARAM_STR" that's why i couldn't sent the id, now everything works. Thx for the tip about the id(pk).
0 new messages