Problem with the "quote" PDO function

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
Hello.

I try to use the quote function of PDO to escape a string issued from a form. The connection with the DB seems to be establish, but I've this message :
Fatal error: Call to a member function quote() on a non-object in /home/user/public_html/functions.php on line 5


that's the function I use :
PHP:
include ('DB_Connect.inc');
include ('functions.php');
$ut = $_POST['utilisateur'];
$pa = safe($_POST['upass']);
the script after that isn't executed because of the error.

That's the included DB_Connect.inc :
PHP:
<?php
//on se connecte à la database
$db  = DB_name;
$dbh = new PDO("mysql:host=localhost;dbname=$db", 'user', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
and the functions.php :
PHP:
<?php
// ce fichier est un include php
function safe($v) {
// return mysql_real_escape_string($var);
$ret = $dbh->quote($v);
return $ret;
}
?>
I don't understand why that doesn't work ! Fore, I used a mysql_real_escape_string with a mysql connection (out of PDO) but all the rest of my script's access to DB was written with PDO. I'd like to uniformize, using only one connection via my include file DB_Connect.inc !!!

Am I on the wrong way ?

Thank you for your help.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You need to declare $dbh as global within function safe($v), or pass $dbh as a parameter.
 
Last edited:

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
Thanks, Misson, that was rather simple ! :nuts:

I declare it as global... and it works !
 
Top