I have a php sript with a simple mysql SELECT query which fetches more than 1000 rows from the database...

It gives a 500 internal server error many times when I have a large number of rows to fetch..

The same script works fine when I run it from unix shell command:

/usr/bin/php  scriptname.php

It's a server with apache + php fcgi.

Please help -- thanks!

  • 1
    Have a look at your apache error log. It will contain more information about the problem. Update your question with what you find. – Iain Jan 13 '11 at 11:59
  • How is PHP configured? CGI/FastCGI/mod_php? – Reece45 Jan 13 '11 at 14:22
  • php configured with fastcgi ... – Amit Aggarwal Jan 13 '11 at 20:28

I think you're probably hitting the Fcgi timeout. How long does the script take to run? What does Apache's error_log say? Have you tried changing the FcgidBusyTimeout Directive in the Apache config?

  • Glad I could help! – Josh Jan 15 '11 at 15:39

Probably your script exceeds timeout limit. You should use set_time_limit function.

  • 1
    I don't think so, if limit is reached, error is printed if allowed, but it never returned 500 - Internal server error to me, seems like apache issue. – Ency Jan 13 '11 at 14:09

How about changing your script, just returning a part of the rows (e.g. 1st 100 rows) and then giving the possibility to fetch the next ones.

Not every problem can be fixed with some configuration parameters.


This can be due to several issues, PHP has different controls to avoid an script to go crazy (max memory and max execution time).

The 3 most common problems associated with a 50x error from your server are

  • Script has timeouted its maximum execution time : You'll see this one because it'll take a long time to reply, one way to avoid this is to override the max execution time in the first line of the script if you know that it'll actually take more than the default max execution
  • Script has used all the available memory : This one will be a lot quicker and possibly it'll be your one, it can be also overriden in case your script needs it
  • Infinite recursive loop : This will be quite violent, and it's the last scenario, normally it's either of the first two, so if neither solution works it's a likely scenario

Good luck with your debugging!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.