SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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 :
    PHP Code:
    $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 :

    Code:
    could not find driver

    but when i write code such as next code then it's work fine !!

    PHP Code:
    $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

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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 ...
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 :$

  4. #4
    SitePoint Zealot Jaanboy's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    $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.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Jan

    thx alot very much .. it's helped me ..

    i tried it

    i configured my code and i made $cc as :

    PHP Code:

    $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 :
    Code:
    Warning: PDO::__construct() expects parameter 4 to be array
    althought of paramter #4 in $cc is array ?

  6. #6
    SitePoint Zealot Jaanboy's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •