SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PDO database connection crashes my apache

    Hi i am going through the book php anthology 101 tips and tricks and my apache sends an error warning when I process this code:

    In particular it does not like this: $dbh->query($sql). I can establish the connection through PDO.

    running PHP version 5.1

    $dsn = 'mysql:host=localhost;dbname=world;';
    $user = 'root';
    $password = '';

    $country = 'USA';

    try
    {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE,
    PDO::ERRMODE_EXCEPTION);
    $sql = 'Select * from city where CountryCode ='.$dbh->quote($country);

    foreach ($dbh->query($sql) as $row)
    {
    print $row['Name'] . "\t";
    print $row['CountryCode'] . "\t";
    print $row['Population'] . "\n";
    }
    }

    catch (PDOException $e)
    {
    echo 'PDO Exception Caught. ';
    echo 'Error with the database: <br />';
    echo 'SQL Query: ', $sql;
    echo 'Error: ' . $e->getMessage();
    }

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you show us the warning that apache gives you?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What happens when you try this instead?

    $sql = 'Select * from city where CountryCode ="USA"';

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried to hard code the sql statement did not really make any difference I also echoed it out and it was suppling the correct sql statement.

    error:

    Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience.

    It does not shut down though?

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so this means that PHP encountered a problem and was closed.

    What I would do is comment out all the code below $dbh = new PDO($dsn, $user, $password); and see if it shows a blank page or Apache error. If it's blank then uncomment another line and so, until you can find the problem line.

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont get the error when I remove the foreach loop. So I removed it and replaced it with:

    $dbh->query($sql);

    And this causes the crash again?

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps try upgrading to the newest version of PHP?

    If it still doesn't work, then you need to submit a ticket to PHP. It's a problem that they would have to fix.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Maybe the drivers aren't correctly installed:

    Relevant dump from my phpinfo();
    Code:
    PDO
    PDO support	enabled
    PDO drivers 	mysql, sqlite
    
    pdo_mysql
    PDO Driver for MySQL, client library version	5.0.51a
    
    pdo_sqlite
    PDO Driver for SQLite 3.x	enabled
    PECL Module version 	1.0.1 $Id: pdo_sqlite.c,v 1.10.2.6.2.3 2007/12/31 07:20:10 sebastian Exp $
    SQLite Library 	3.3.7 undefined

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is my phpinfo. I imagine this is the latest as I had a new pc and insstalled xampp yesterday.

    PDO
    PDO support enabled
    PDO drivers mssql, mysql, sqlite2

    pdo_mssql
    PDO Driver for MSSQL DB-lib enabled
    Flavour MSSQL_70

    pdo_mysql
    PDO Driver for MySQL, client library version 5.1.30

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That looks ok to me.

    I wonder if its a pdo returning an object issue, tell it to explicitly use the pdostatement class and tell that to return an array.

    PHP Code:
    $stmt $dbh->query($sql) ;

    foreach ( 
    $stmt->fetchAllPDO::FETCH_BOTH ) as $row )
    {
    print 
    $row['Name'] . "\t";
    print 
    $row['CountryCode'] . "\t";
    print 
    $row['Population'] . "\n";

    If not I'm stumped, apart from var_dumping things.

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at your apache and mysql error logs, maybe something in there will give a hint.

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The newest version of PHP is 5.2.8. According to you, you are running 5.1.

  13. #13
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups
    Tried your code:
    $stmt = $dbh->query($sql) ;

    foreach ( $stmt->fetchAll( PDO::FETCH_BOTH ) as $row )
    {
    print $row['Name'] . "\t";
    print $row['CountryCode'] . "\t";
    print $row['Population'] . "\n";
    }

    This still caused a crash.

    Tried changing the sql statement:

    $sql = 'Select Name from city where CountryCode ='.$dbh->quote($country);

    Tried other fields and they worked too. But as soon as I added a comma to add another field a crash occurred?

    Where do I go to view the error logs?

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you saying this works;
    PHP Code:
    $sql 'Select Name from city where CountryCode ='.$dbh->quote($country);
    $stmt $dbh->query($sql) ;
    $stmt->fetchAllPDO::FETCH_BOTH );
    var_dump$stmt ); 
    But this doesnt work?
    PHP Code:
    $sql 'Select Name, CountryCode from city where CountryCode ='.$dbh->quote($country);
    $stmt $dbh->query($sql) ;
    $stmt->fetchAllPDO::FETCH_BOTH );
    var_dump$stmt ); 
    If so, show us the output of those 2 along with the results of the sql statement "describe table city"

  15. #15
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know im a bit late here and this topic is probably ancient now, but i have a very relevant issue. The crashes are exactly what i am experiencing. Ive got the newest php,mysql,apache versions, even reinstalled them all but this query works
    PHP Code:
    $sql "SELECT USERNAME FROM USERS WHERE USERNAME='aimbot'";
            
    $stmt $db1->query($sql);
            
    $obj $stmt->fetch(PDO::FETCH_OBJ); 
    however this doesnt
    PHP Code:
    $sql "SELECT USERNAME, NAME FROM USERS WHERE USERNAME='aimbot'";
            
    $stmt $db1->query($sql);
            
    $obj $stmt->fetch(PDO::FETCH_OBJ); 
    BUT this works
    PHP Code:
    $sql "SELECT NAME FROM USERS WHERE USERNAME='aimbot'";
            
    $stmt $db1->query($sql);
            
    $obj $stmt->fetch(PDO::FETCH_OBJ); 
    ive got no idea. Maybe we are both doing something wrong. Thanks. Im on a fresh windows install.

  16. #16
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are your column names really all uppercase?

    try this:

    var_dump( $stmt );

    and likewise, tell us what describe table tells you about that table.

  17. #17
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What hasn't been mentioned is that the error message is a Windows application crash message. I suspect you're loading the wrong PHP module in your httpd.conf file. If you see the line:

    LoadModule php5_module C:/path/to/PHP/php5apache.dll

    try changing it to:

    LoadModule php5_module C:/path/to/PHP/php5apache2_2_filter.dll

    or one of the other php5apache*.dll files in your PHP directory.

  18. #18
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no they arent all uppercase and i have no idea why this fixed it but i drag and dropped php v.5.2.6 into my existing php location. Replaced my php.ini and restarted my apache server and everyone just worked.

  19. #19
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was probably, like I said, an old PHP version. PHP has these kind of bugs from time to time.

  20. #20
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a bug in xampp version 1.7.0.

    There is a fix for it but stupid form won't let me post the link to it.

  21. #21
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Quote Originally Posted by dr.evil View Post
    It's a bug in xampp version 1.7.0.

    There is a fix for it but stupid form won't let me post the link to it.
    Here's the link, the instructions provided solved my issue with my PDO too.
    (they won't let me post the link either. Checkout the attached screenshot of the url :P)

    Excerpts: 1. Download php-5.2-win32-VC6-x86-latest (around 10MB)
    2. Replace the 'libmysql.dll' file
    Attached Images Attached Images


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
  •