Why does PHP throw "Segmentation fault (11)" after "mysql_connect", or upon "mysql_close"?

19,403

Solution 1

Smells like your mysql install. Is mysql running as a service? Does your root account really have no password? What mysql extensions are loaded in php.ini? There's a php_mssql.dll which can easily be mistaken for php_msql.dll, but I'm fairly certain the php_mssql.dll is for windows only.

Have you edited your php.ini /opt/lampp/php/php.ini? These may help with debugging:

error_reporting  =  E_ALL & ~E_NOTICE
display_errors = On
display_startup_errors = On ;but you'll want to turn this off soon ;)
mysql.trace_mode = On

Also worth looking at, the port number used. Is $MYSQL_TCP_PORT set? How about the mysql-tcp entry in /etc/services?

Update: Have you tried checking the return value of the connect function? I'm wondering if you example is too minimal. Try making your test look like this:

<?php

$db = mysql_connect("localhost","root","myrootpassword");
if (!$db) {
    die('Could not connect: ' . mysql_error());
}
else {
    $result = mysql_query("show databases");
   print "
   <h1>Test executed from ". $_SERVER['SCRIPT_NAME']. "</h1>

    \n";
    print "Script name: ". $_SERVER['SCRIPT_FILENAME'] ." <hr>\n";
    while ($line = mysql_fetch_array($result))
    {
        print "$line[0]<br>\n";
    }
}       
mysql_close($db);

?> 

This example ensure you send something back that looks like HTML whether your script fails or not. I was able to reproduce your symptoms with no root password set, so it's apparently not only good practice, but it's required to be non-empty.

I was able to turn your problem into a a solution by

  1. Setting the root password
  2. returning something from the script on connect

Instead of passing in "" as the root password, if it really is nothing, try using mysql.default_password as the password. The default value for mysql.default_password is NULL.

Solution 2

This could be a hardware problem or an Apache/PHP configuration problem. I've usually seen Signal 11 with memory problems. Look at your configs to make sure that the PHP memory limit isn't higher than the Apache memory limit or similar.

It sounds like your php_mod is crashing mid-program, so Apache is just tossing the php file at you with a non-html content-type header, which makes your browser treat it as a download. That's just a guess, though. You need to gather some more information.

Add some error_log() calls to your PHP program so that you can isolate when the crash actually occurs in your code. If that doesn't help narrow it down, set up Apache to create a core dump and use gdb to figure out where the segfault starts. My bet is mod_php.so.

Solution 3

You say it's all up and running and that simple files are showing up okay. But then some files are prompting you to save the PHP file. This means you'll have to look at the code of those pages where it's breaking and asking you to save.

It's more than likely you have a line in the code that is killing the server or just making it give up parsing the PHP.

This could be a particular extension that's being used by that page or it could be another killbot function that hobbles the server into just sitting there with its arms crossed.

Your error logs show that the last thing to happen before it shoves the save dialog in your face is a connect to the database. Follow the code back to that and make sure that's solid before moving onto the next possible troubleshooting breadcrumb that may or may not arise.

Since you can see the file with no problem on another set up, in this case easyPHP, the problem is very much on the XAMPP set up and a configuration that is being tripped up by the code.

Share:
19,403

Related videos on Youtube

Shawn
Author by

Shawn

Updated on September 17, 2022

Comments

  • Shawn
    Shawn over 1 year

    I have Ubuntu-9.04 and am using XAMPP-1.7.2 to develop a web application. The problem is that when I try to view a PHP file I wrote by visiting localhost/folder/file.php, Firefox offers me to download it instead of showing me the file as a web page. If I do download it and open it in my favorite text editor, I get an empty file with nothing in it...

    UPDATE: Works on another server Well, I uploaded the page to another server, and I can see it fine! So the problem is not with Firefox. Nor is it with the code itself. My server or its configuration must be the problem. Does that give any of you a clue as to what's happening?

    UPDATE: Isolating the problem Firefox offers to download only the first of these three files:

    test1.php is offered as a download

    <?php
        mysql_connect("localhost","root","");
        mysql_close();
    ?>
    

    test2.php outputs: Warning: mysql_close(): no MySQL-Link resource supplied in /opt/lampp/htdocs/test.php on line 3

    <?php
        //mysql_connect("localhost","root","");
        mysql_close();
    ?>
    

    test3.php outputs a blank page

    <?php
        mysql_connect("localhost","root","");
        //mysql_close();
    ?>
    

    I know that my server is up and running and it tells me that PHP5 is on, and running as well.

    Here is what I get in the XAMPP error log when the problem occurs (truncated and formatted for clarity):

    [notice] child pid 7338 exit signal Segmentation fault (11)
    

    Oh, and firefox can see the file without a problem on my other computer (windows XP SP3 and easyPHP as server) Does anyone know what I can do to solve this problem?

    UPDATE: using error_log()

    Here is my try at using error_log(): test4.php

    <?php
        error_log("Start of file reached by PHP");
        mysql_connect("localhost","root","");
        error_log("mysql_connect executed");
        mysql_close();
        error_log("mysql_close executed");
    ?>
    

    This is what it produces in the XAMPP error log (trucated for clarity):

    [error] [client 127.0.0.1] Start of file reached by PHP
    [error] [client 127.0.0.1] mysql_connect executed
    [notice] child pid 5338 exit signal Segmentation fault (11)
    
    • Arjan
      Arjan over 14 years
      I am not sure if error_log enforces synchronous log files: messages from multiple sources might not appear in the exact order in which the messages are generated. So, though not likely, the actual error might have occurred before error_log was executed. (And though the download that Firefox tries is not your problem, you could try to see what HTTP headers are sent along with that file. Especially the Content-Type will probably tell you what the browser thinks it received. See, for example, the Net panel in Firebug: getfirebug.com/net.html)
    • Brian Ortiz
      Brian Ortiz over 14 years
      Shouldn't this be on Stack Overflow?
    • Shawn
      Shawn over 14 years
      I tried looking at the HTTP headers, but when Firefox offers to download a page, nothing appears under the Net tab. So I can't even look at the headers... What does this mean about how firefox sees the file?
  • Shawn
    Shawn over 14 years
    So how do I determine what's wrong in the XAMPP set up and configuration? Or how do I determine what's wrong in my code that messes up XAMPP?
  • Arjan
    Arjan over 14 years
    @Shawn, what about fixing that Access denied for user 'user'@'localhost' in your MySQL set up...?
  • Christoph Rüegg
    Christoph Rüegg over 14 years
    @Shawn Arjan is right, start debugging from the line where it says you can't log into the database. Your settings are probably not the same between XAMPP and easyPHP and this includes database setup.
  • Shawn
    Shawn over 14 years
    Well, I solved the Access denied problem, now I only get the Segmentation fault (I edited my question)
  • Shawn
    Shawn over 14 years
    I tried using error_log by adding <?php error_log("Start of file reached by PHP\n", 3, "/opt/lampp/htdocs/webÉchange/SiteWeb_V5/errorLog.txt"); ?> at the very beggining of one of the faulty files, but errorLog.txt remains blank. I also tried using <?php error_log("End of file reached by PHP", 1, "[email protected]", "From: [email protected]"); ?>, [email protected] being my own e-mail, but still, nothing happens. As for setting up Apache, I can't seem to find how to do this. Actually, I'm finding very little information about setting anything in XAMPP...
  • Shawn
    Shawn over 14 years
    I read up on the core dump, and frankly, I can't understand it. I know I can break my system if I do too many things I don't understand in the command line, so I am hesitant to go on this particular journy. I have edited my question above because I have managed to isolate where the problem occurs. Could you check it out and tell me if the core dump really is what I need? Thanks
  • Shawn
    Shawn over 14 years
    How can I tell if mysql is running as a service? I don't know if my root has a password or not, but I've tried the sample code with many different users, with and without passwords, to no avail. As for extensions, I haven't installed any since I first installed XAMPP. I can't find php_mssql.dll anywhere. I tried echo($MYSQL_TCP_PORT); here is what I get Notice: Undefined variable: MYSQL_TCP_PORT in /opt/lampp/htdocs/test.php on line 2 The other settings in php.ini change nothing to my problem.
  • mikedub
    mikedub over 14 years
    I'd guess that there's a permissions problem with creating errorLog.txt. Why not drop the second and third arguments? Then your message will be added to your default error_log file. If you're not comfortable doing the core dump, then you probably shouldn't. My next question is: what shows up in the test1.php file after you download it?
  • Shawn
    Shawn over 14 years
    I tried error_log with only one argument and it worked. I edited my original post, adding the results of my try. As for what's in the downloaded file: nothing. I download it and open it in gedit, and I get a blank file (0 bytes, btw). That too was edited in my original post. As you can see, it executes everything up to mysql_close(); then it crashes. Yet, the file is blank upon download.. What does this mean? Thanks very much for your help up to now.
  • Shawn
    Shawn over 14 years
    That's interesting. I had always been wondering why there is no entry for XAMPP in the applications tab. Indeed, I can't find the Others link which you speak of. In applications, I have only Accessories, Games, Graphics, Internet, Office, Programming, Sound & Video, System tools and Add/Remove... And I can't find anything XAMPP-related. I just start XAMPP with sudo /opt/lampp/lampp start
  • Shawn
    Shawn over 14 years
    Quick security check... Your XAMPP pages are NOT secured by a password. Do you want to set a password? [yes] no MySQL is accessable via network. Normaly that's not recommended. Do you want me to turn it off? [yes] yes ... The MySQL/phpMyAdmin user pma has no password set!!! Do you want to set a password? [yes] yes ... Setting new MySQL pma password. Setting phpMyAdmin's pma password to the new one. MySQL has no root passwort set!!! Do you want to set a password? [yes] no The FTP password for user 'nobody' is still set to 'lampp'. Do you want to change the password? [yes] no Done. that good?
  • DaveParillo
    DaveParillo over 14 years
    Can you connect using phpMyAdmin?
  • mikedub
    mikedub over 14 years
    Well, it isolates the problem to the MySQL client in PHP. I'd try the code from Example #1 on the mysql_close man page[us.php.net/mysql_close] to see if your connection actually gets created or whether the mysql_close call is causing the segfault.
  • DaveParillo
    DaveParillo over 14 years
    Shawn - I was able to reproduce your problem & have a solution. There was nothing wrong with your install. It's the code. Checkout my updated answer.
  • Shawn
    Shawn over 14 years
    Example #1 on mysql_close man page works fine. It displays "Connected Successfully".
  • Shawn
    Shawn over 14 years
    Connecting with phpMyAdmin works. I tried the last code you posted. Everything works, I get a listing of all databases. But after that, where the mysql_close() statement takes place, I get a "Warning: Unknown: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0". I tried modifying your code slightly by simply taking the parameter out of the mysql_close($db) statement (using mysql_close() instead) and that results in Firefox offering to download the file! What does this mean?
  • DaveParillo
    DaveParillo over 14 years
    When firefox offers to download a file, it's because it doesn't know how to render your page in the browser. In this case, probably because there is no output. Actually, my code would be more correct to have the close statement inside the else. I don't see this error on my install, also on 9.04. Strictly speaking, the close isn't needed as you are not opening a persistent connection. Does removing the close line help?
  • Shawn
    Shawn over 14 years
    The same script without the mysql_close(); statement works as it did when mysql_close($db);: The page is shown WITH the Warning. So I guess the Warning doesn't concern the mysql_close statement. Maybe it concerns the automatic closing of the connection?
  • DaveParillo
    DaveParillo over 14 years
    Even when I reproduced your other symptoms, I never saw this particular warning - I thought your problems were mostly a) you thought your connection was core dumping and b) firefox was not executing your script, but offering to save it. The bottom line is that the mysql_close() call in not needed in this situation. See the php page in freeing resources: us.php.net/manual/en/….
  • DaveParillo
    DaveParillo over 14 years
    If warnings are still appearing on your web pages, you may have left display_errors = On set in your php.ini file.
  • Shawn
    Shawn over 14 years
    Do you think that my using mysql_close when it's not needed is causing the problem? And if so, why on my server and not on another (and not on the server I set up on my other computer)? As for the errors, why am I getting them? They obviously indicate that something is wrong, so I would like to solve them instead of just saying "don't display them". Would there perhaps be a reference page where error messages are explained in more detail?
  • DaveParillo
    DaveParillo over 14 years
    Start with the us.php.net/mysql_close man page. It states that you usually don't need to call mysql_close and that if there is no resource to free when called, it throws an error. Your other installations are using different OS's and possibly different php versions. Frankly, different behavior, especially in how freeing resources is handled would not suprise me.
  • Shawn
    Shawn over 14 years
    Oh, I think I finally understand that getting that warning IS the NORMAL behaviour. Would that be because I have accessed each returned row with mysql_fetch_array, so no other resources need to be freed? Yet in terminating the script, php still checks for any non-freed resource.. Did I get that right?
  • DaveParillo
    DaveParillo over 14 years
    You got it! 8-)