e_2512
July 20, 2010, 11:34pm
1
hi …
i wrote a little code to define pdo library
i wrote a php code to pass parameters :
$c = "PDO";
$cc = "\\"mysql:host=localhost;port=;dbname=ex\\",'root','123456',array(PDO::ATTR_PERSISTENT => 0,PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',PDO::MYSQL_ATTR_INIT_COMMAND => 'SET CHARACTER SET UTF8')";
try{
new $c($cc);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
but it’s didn’t define pdo oop and told me this error message :
could not find driver
but when i write code such as next code then it’s work fine !!
$c = "PDO";
$cc = "\\"mysql:host=localhost;port=;dbname=ex\\",'root','123456',array(PDO::ATTR_PERSISTENT => 0,PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',PDO::MYSQL_ATTR_INIT_COMMAND => 'SET CHARACTER SET UTF8')";
try{
new $c("mysql:host=localhost;port=;dbname=ex",'root','123456',array(PDO::ATTR_PERSISTENT => 0,PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',PDO::MYSQL_ATTR_INIT_COMMAND => 'SET CHARACTER SET UTF8'));
}
catch(PDOException $e)
{
echo $e->getMessage();
}
i don’t know that is the problem !!! … instead of pdo and pdo_mysql are installed and work fine in php.ini !!
plz help:(
No, it’s a string because you have quotes around it. You need to use the same syntax you used to create the $cc array.
e_2512
July 21, 2010, 2:15am
3
Hello Jan
thx alot very much … it’s helped me …
i tried it
i configured my code and i made $cc as :
$c = "PDO";
$cc = array(
"\\"mysql:host=".confitem('database','host').";port=".confitem('database','port').";dbname=".confitem('database','name')."\\"" ,
"'".confitem('database','username')."'" ,
"'".confitem('database','password')."'" ,
"array(PDO::ATTR_PERSISTENT => ".confitem('database','pconnect').",PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ".confitem('database','charset')."',PDO::MYSQL_ATTR_INIT_COMMAND => 'SET CHARACTER SET ".confitem('database','charset')."')"
);
$Reflection = new ReflectionClass($c);
try{
$PDO = $Reflection->newInstanceArgs($cc);
}catch (PDOException $e){
echo $e->getMessage();
}
but it’s gave me this error :
Warning: PDO::__construct() expects parameter 4 to be array
althought of paramter #4 in $cc is array ?
with PDO althought of __construct function can’t called statically
That is true. If you really want to do it this way, then you’ll have to use reflection:
$Reflection = new ReflectionClass($c);
try
{
$PDO = $Reflection->newInstanceArgs($cc);
}
catch (PDOException $e)
{
echo $e->getMessage();
}
Edit: The other option would be to extend the PDO class to allow you to use call_user_func_array.
e_2512
July 21, 2010, 12:08am
5
hello ScallioXTX
really thx alot . i understood what u mean . and i tried to do it with PDO OOP but i faild ! …
now how i can use this function when $cc is array . with PDO althought of __construct function can’t called statically ?
plz can u write the code if u imagine $cc is array ?
thx
rpkamp
July 20, 2010, 11:53pm
6
By passing a string to a constructor like that you’re doing exactly that: passing a string to a constructor.
The way you’re doing it you seem to expect that PHP will figure out that the string contains separate parameters and treat them as such, which it can’t
If you want to do this you should make $cc an array and use call_user_func_array . Although needing that function usually indicates you’re doing something wrong …