This problem will make me crazy ! please help :$!

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.

Hello Jan :smiley:

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.

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 :blush:

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 :slight_smile:

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 …