3 related problems but mainly about PDO connection

Discussion in 'PHP' started by Sohila, Feb 14, 2010.

  1. #1
    hi
    I am building my 1st php website, I am trying to build a database connection using PDO.

    1st I made the connection using the following syntax


    $db = new PDO("dbname=****;mysql:host=localhost" , 'username','password');
    PHP:

    but when I tested the page, an error appeared in the explorer declaring that class PDO wasn't found in the project's files.

    then I tried to create a function that manage the connection. my syntax was :

    <?php
    function db(){
    $db = new PDO("dbname=****;mysql:host=localhost" , 'username','password');
    }
    ?>
    PHP:
    This way the error disappears, but actually I can't test the connection.I tried to test by 2 ways:

    * to echo a statement declaring the ststus of the connection either successfull or not like the following:
    <?php
    function db(){
    try{
        ("dbname=****;mysql:host=localhost" , 'username','password');
    echo "Success";
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
    }
    ?>
    PHP:
    but nothing got printed!! that leads us to the question

    Should PDO connection created within a function or that's not the normal use?
    and
    How to print within a function?


    The 2nd way I tried to test is by redirecting to another page by setting the html header
    header(Location: "destination.php");

    ofcourse I tested it within the function to avoid the 1st error, but still nothing and doesn't redirect..... I changed the file type from plain PHP file and HTML file and nothing.
    and the question here is :
    How to redirect within a function, and can it be within a php file not HTML page or not?
    Noting that there's no output sent to the browser previous than this redirection: it just receives some paramteters


    complicated problems and don't know how to fix any.... can any one help?

    Thank you in advance
     
    Sohila, Feb 14, 2010 IP
  2. TheSpringRoad

    TheSpringRoad Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    with PHP5 pdo extension not actived by default so you have to do this
    by yourself open the 'php.ini' file then check this linge:

    ;extension=php_pdo.dll

    now remove the semicolon.

    after this you have to active the driver so search for this ligne:

    ;extension=php_pdo_mysql.dll

    if it exists just remove the semicolon else add it
     
    TheSpringRoad, Feb 15, 2010 IP